Skip to content

Commit 955df34

Browse files
committed
Update PyPDF2 imports, fix import resolution, and replace deprecated methods
1. Updated import statements for PyPDF2 library to use PdfReader and PdfWriter, as PdfFileReader and PdfFileWriter were deprecated and removed in PyPDF2 3.0.0. 2. Fixed the import resolution for ContentStream in PyPDF2 by importing it from PyPDF2.generic instead of PyPDF2.pdf, resolving a "MissingImports" error. 3. Replaced the deprecated method 'getObject' with 'get_object' in PDF page iteration, ensuring compatibility with the latest version of PyPDF2. 4. Corrected the syntax for byte string comparison in the PDF operator check, replacing b_('Tf') with the correct syntax b'Tf', eliminating a syntax error. These changes ensure compatibility with the latest version of PyPDF2, remove deprecated features, and resolve import and syntax issues in the code.
1 parent 05c723c commit 955df34

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

Diff for: remover.py

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
from PyPDF2 import PdfFileReader, PdfFileWriter
4-
from PyPDF2.pdf import ContentStream
3+
# PyPDF2 - a Python library that allows reading, writing, and manipulating PDF files
4+
from PyPDF2 import PdfReader, PdfWriter
55
from PyPDF2.generic import NumberObject, TextStringObject, NameObject
6-
from PyPDF2.utils import b_
76

7+
from PyPDF2.generic import ContentStream
8+
9+
# tkinter - used for building the GUI (Graphical User Interface) application
810
from tkinter import Tk, Label, Button, StringVar
911
from tkinter.filedialog import askopenfilename, asksaveasfilename, askdirectory
1012
from tkinter.constants import N,S,W,E, LEFT, TOP, RIGHT, BOTTOM
1113
import sys, os
1214
import tkinter.font as font
15+
16+
# reportlab - a library for generating PDFs programmatically
1317
from reportlab.pdfgen import canvas
1418

1519
def resource_path(relative_path):
@@ -18,7 +22,7 @@ def resource_path(relative_path):
1822
return os.path.join(os.path.abspath("."), relative_path)
1923

2024

21-
class PdfEnhancedFileWriter(PdfFileWriter):
25+
class PdfEnhancedFileWriter(PdfWriter):
2226

2327
colors_operands = {
2428
'rgb': {
@@ -37,23 +41,20 @@ class PdfEnhancedFileWriter(PdfFileWriter):
3741

3842
def _getOperatorType(self, operator):
3943
operator_types = {
40-
b_('Tj'): 'text',
41-
b_("'"): 'text',
42-
b_('"'): 'text',
43-
b_("TJ"): 'text',
44-
45-
b_('rg'): 'rgb', # color
46-
b_('RG'): 'rgb', # color
47-
b_('k'): 'cmyk', # color
48-
b_('K'): 'cmyk', # color
49-
b_('g'): 'grayscale', # color
50-
b_('G'): 'grayscale', # color
51-
52-
b_('re'): 'rectangle',
53-
54-
b_('l'): 'line', # line
55-
b_('m'): 'line', # start line
56-
b_('S'): 'line', # stroke(paint) line
44+
(b"Tj"): "text",
45+
(b"'"): "text",
46+
(b'"'): "text",
47+
(b"TJ"): "text",
48+
(b"rg"): "rgb", # color
49+
(b"RG"): "rgb", # color
50+
(b"k"): "cmyk", # color
51+
(b"K"): "cmyk", # color
52+
(b"g"): "grayscale", # color
53+
(b"G"): "grayscale", # color
54+
(b"re"): "rectangle",
55+
(b"l"): "line", # line
56+
(b"m"): "line", # start line
57+
(b"S"): "line", # stroke(paint) line
5758
}
5859

5960
if operator in operator_types:
@@ -86,11 +87,10 @@ def removeWordStyle(self, ignoreByteStringObject=False):
8687
to ignore ByteString Objects.
8788
"""
8889

89-
pages = self.getObject(self._pages)['/Kids']
90-
for j in range(len(pages)):
91-
page = pages[j]
92-
pageRef = self.getObject(page)
93-
content = pageRef['/Contents'].getObject()
90+
pages = self.get_object(self._pages)['/Kids']
91+
for page in pages:
92+
pageRef = self.get_object(page)
93+
content = pageRef["/Contents"].get_object()
9494

9595
if not isinstance(content, ContentStream):
9696
content = ContentStream(content, pageRef)
@@ -100,25 +100,25 @@ def removeWordStyle(self, ignoreByteStringObject=False):
100100

101101
for operator_index, (operands, operator) in enumerate(content.operations):
102102

103-
if operator == b_('Tf') and operands[0][:2] == '/F':
103+
if operator == (b'Tf') and operands[0][:2] == '/F':
104104
last_font_size = operands[1].as_numeric()
105105

106-
if operator == b_('Tj'):
106+
if operator == (b'Tj'):
107107
text = operands[0]
108108
if ignoreByteStringObject:
109109
if not isinstance(text, TextStringObject):
110110
operands[0] = TextStringObject()
111-
elif operator == b_("'"):
111+
elif operator == (b"'"):
112112
text = operands[0]
113113
if ignoreByteStringObject:
114114
if not isinstance(text, TextStringObject):
115115
operands[0] = TextStringObject()
116-
elif operator == b_('"'):
116+
elif operator == (b'"'):
117117
text = operands[2]
118118
if ignoreByteStringObject:
119119
if not isinstance(text, TextStringObject):
120120
operands[2] = TextStringObject()
121-
elif operator == b_("TJ"):
121+
elif operator == (b"TJ"):
122122
for i in range(len(operands[0])):
123123
if ignoreByteStringObject:
124124
if not isinstance(operands[0][i], TextStringObject):
@@ -149,7 +149,7 @@ def removeWordStyle(self, ignoreByteStringObject=False):
149149
# remove styled rectangles (highlights, lines, etc.)
150150
# the 're' operator is a Path Construction operator, creates a rectangle()
151151
# presumably, that's the way word embedding all of it's graphics into a PDF when creating one
152-
if operator == b_('re'):
152+
if operator == (b're'):
153153

154154
rectangle_width = operands[-2].as_numeric()
155155
rectangle_height = operands[-1].as_numeric()
@@ -192,7 +192,7 @@ def createMultiPage(file_path):
192192

193193
def load_pdf(filename):
194194
f = open(filename,'rb')
195-
return PdfFileReader(f)
195+
return PdfReader(f)
196196

197197
def load1():
198198
f = askopenfilename(multiple=True, filetypes=(('PDF File', '*.pdf'), ('All Files', '*.*')))
@@ -214,7 +214,7 @@ def load1():
214214
#print(pdf_list)
215215

216216
def add_to_writer(pdfsrc, writer):
217-
[writer.addPage(pdfsrc.getPage(i)) for i in range(pdfsrc.getNumPages())]
217+
[writer.add_page(pdfsrc.pages[i]) for i in range(len(pdfsrc.pages))]
218218
writer.removeWordStyle()
219219

220220
def remove_images():

0 commit comments

Comments
 (0)