diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index 26c84dc46..1f37ab6ff 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -923,7 +923,7 @@ def compress_content_streams(self): if content is not None: if not isinstance(content, ContentStream): content = ContentStream(content, self.pdf) - self[NameObject(PG.CONTENTS)] = content.flateEncode() + self[NameObject(PG.CONTENTS)] = content.flate_encode() def compressContentStreams(self): """ diff --git a/PyPDF2/_reader.py b/PyPDF2/_reader.py index 6680ca2ac..6fc8bd587 100644 --- a/PyPDF2/_reader.py +++ b/PyPDF2/_reader.py @@ -389,7 +389,7 @@ def xmp_metadata(self): """ try: self._override_encryption = True - return self.trailer[TK.ROOT].getXmpMetadata() + return self.trailer[TK.ROOT].xmp_metadata finally: self._override_encryption = False @@ -729,9 +729,9 @@ def getNamedDestinations(self, tree=None, retval=None): @property def outlines(self): """Read-only property.""" - return self.get_outlines() + return self._get_outlines() - def get_outlines(self, node=None, outlines=None): + def _get_outlines(self, node=None, outlines=None): """ Retrieve the document outline present in the document. @@ -768,7 +768,7 @@ def get_outlines(self, node=None, outlines=None): # check for sub-outlines if "/First" in node: sub_outlines = [] - self.get_outlines(node["/First"], sub_outlines) + self._get_outlines(node["/First"], sub_outlines) if sub_outlines: outlines.append(sub_outlines) @@ -778,18 +778,33 @@ def get_outlines(self, node=None, outlines=None): return outlines + def get_outlines(self, node=None, outlines=None): + """ + .. deprecated:: 1.28.0 + + Use the property :py:attr:`outlines` instead. + """ + warnings.warn( + "get_outlines will be removed in PyPDF2 2.0.0. " + "Use the property :py:attr:`outlines` instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self._get_outlines(node, outlines) + def getOutlines(self, node=None, outlines=None): """ .. deprecated:: 1.28.0 - Use :meth:`get_outlines` instead. + Use the property :py:attr:`outlines` instead. """ warnings.warn( - "getOutlines will be removed in PyPDF2 2.0.0. Use get_outlines instead.", + "getOutlines will be removed in PyPDF2 2.0.0. " + "Use the property 'outlines' instead.", PendingDeprecationWarning, stacklevel=2, ) - return self.get_outlines(node, outlines) + return self._get_outlines(node, outlines) def _get_page_number_by_indirect(self, indirect_ref): """Generate _pageId2Num""" @@ -1073,7 +1088,7 @@ def _get_object_from_stream(self, indirect_reference): assert obj_stm["/Type"] == "/ObjStm" # /N is the number of indirect objects in the stream assert idx < obj_stm["/N"] - stream_data = BytesIO(b_(obj_stm.getData())) + stream_data = BytesIO(b_(obj_stm.get_data())) for i in range(obj_stm["/N"]): readNonWhitespace(stream_data) stream_data.seek(-1, 1) @@ -1515,7 +1530,7 @@ def _read_pdf15_xref_stream(self, stream): xrefstream = read_object(stream, self) assert xrefstream["/Type"] == "/XRef" self.cache_indirect_object(generation, idnum, xrefstream) - stream_data = BytesIO(b_(xrefstream.getData())) + stream_data = BytesIO(b_(xrefstream.get_data())) # Index pairs specify the subsections in the dictionary. If # none create one subsection that spans everything. idx_pairs = xrefstream.get("/Index", [0, xrefstream.get("/Size")]) diff --git a/PyPDF2/_writer.py b/PyPDF2/_writer.py index f4144ee9f..9db67416a 100644 --- a/PyPDF2/_writer.py +++ b/PyPDF2/_writer.py @@ -966,7 +966,7 @@ def add_bookmark_destination(self, dest, parent=None): parent = outline_ref parent = parent.get_object() - parent.addChild(dest_ref, self) + parent.add_child(dest_ref, self) return dest_ref @@ -1002,7 +1002,7 @@ def add_bookmark_dict(self, bookmark, parent=None): parent = outline_ref parent = parent.get_object() - parent.addChild(bookmark_ref, self) + parent.add_child(bookmark_ref, self) return bookmark_ref @@ -1053,7 +1053,7 @@ def add_bookmark( dest = Destination( NameObject("/" + title + " bookmark"), page_ref, NameObject(fit), *zoom_args ) - dest_array = dest.getDestArray() + dest_array = dest.dest_array action.update( {NameObject("/D"): dest_array, NameObject("/S"): NameObject("/GoTo")} ) @@ -1089,7 +1089,7 @@ def add_bookmark( bookmark_ref = self._add_object(bookmark) parent = parent.get_object() - parent.addChild(bookmark_ref, self) + parent.add_child(bookmark_ref, self) return bookmark_ref @@ -1470,7 +1470,7 @@ def add_link(self, pagenum, pagedest, rect, border=None, fit="/Fit", *args): dest = Destination( NameObject("/LinkName"), page_dest, NameObject(fit), *zoom_args ) # TODO: create a better name for the link - dest_array = dest.getDestArray() + dest_array = dest.dest_array lnk = DictionaryObject() lnk.update( diff --git a/PyPDF2/filters.py b/PyPDF2/filters.py index 60051b4b9..207a95281 100644 --- a/PyPDF2/filters.py +++ b/PyPDF2/filters.py @@ -553,7 +553,7 @@ def _xobj_to_image(x_object_obj): from PyPDF2.constants import GraphicsStateParameters as G size = (x_object_obj[IA.WIDTH], x_object_obj[IA.HEIGHT]) - data = x_object_obj.getData() + data = x_object_obj.get_data() if x_object_obj[IA.COLOR_SPACE] == ColorSpaces.DEVICE_RGB: mode = "RGB" else: @@ -564,7 +564,7 @@ def _xobj_to_image(x_object_obj): extension = ".png" img = Image.frombytes(mode, size, data) if G.S_MASK in x_object_obj: # add alpha channel - alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].getData()) + alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].get_data()) img.putalpha(alpha) img_byte_arr = io.BytesIO() img.save(img_byte_arr, format="PNG") diff --git a/PyPDF2/generic.py b/PyPDF2/generic.py index e95d0abe6..613e99891 100644 --- a/PyPDF2/generic.py +++ b/PyPDF2/generic.py @@ -704,7 +704,8 @@ def setdefault(self, key, value=None): def __getitem__(self, key): return dict.__getitem__(self, key).get_object() - def getXmpMetadata(self): + @property + def xmp_metadata(self): """ Retrieve XMP (Extensible Metadata Platform) data relevant to the this object, if available. @@ -725,15 +726,31 @@ def getXmpMetadata(self): self[NameObject("/Metadata")] = metadata return metadata + def getXmpMetadata(self): # XmpInformation + """ + .. deprecated:: 1.28.3 + Use :meth:`xmp_metadata` instead. + """ + warnings.warn( + "getXmpMetadata will be removed in PyPDF2 2.0.0. " + "Use xmp_metadata instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.xmp_metadata + @property def xmpMetadata(self): """ - Read-only property that accesses the {@link - #DictionaryObject.getXmpData getXmpData} function. -

- Stability: Added in v1.12, will exist for all future v1.x releases. + .. deprecated:: 1.28.3 + Use :meth:`xmp_metadata` instead. """ - return self.getXmpMetadata() + warnings.warn( + "xmpMetadata will be removed in PyPDF2 2.0.0. Use xmp_metadata instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.xmp_metadata def write_to_stream(self, stream, encryption_key): stream.write(b_("<<\n")) @@ -888,6 +905,14 @@ def children(self): child = child["/Next"] def addChild(self, child, pdf): + warnings.warn( + DEPR_MSG.format("addChild", "add_child"), + PendingDeprecationWarning, + stacklevel=2, + ) + self.add_child(child, pdf) + + def add_child(self, child, pdf): # PdfReader child_obj = child.get_object() child = pdf.getReference(child_obj) assert isinstance(child, IndirectObject) @@ -913,6 +938,14 @@ def addChild(self, child, pdf): child_obj[NameObject("/Parent")] = parent_ref def removeChild(self, child): + warnings.warn( + DEPR_MSG.format("removeChild", "remove_child"), + PendingDeprecationWarning, + stacklevel=2, + ) + self.remove_child(child) + + def remove_child(self, child): child_obj = child.get_object() if NameObject("/Parent") not in child_obj: @@ -1000,7 +1033,25 @@ def emptyTree(self): class StreamObject(DictionaryObject): def __init__(self): self._data = None - self.decodedSelf = None + self.decoded_self = None + + @property + def decodedSelf(self): + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) + return self.decoded_self + + @decodedSelf.setter + def decodedSelf(self, value): + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) + self.decoded_self = value def write_to_stream(self, stream, encryption_key): self[NameObject(SA.LENGTH)] = NumberObject(len(self._data)) @@ -1026,6 +1077,14 @@ def initializeFromDictionary(data): return retval def flateEncode(self): + warnings.warn( + DEPR_MSG.format("flateEncode", "flate_encode"), + PendingDeprecationWarning, + stacklevel=2, + ) + return self.flate_encode() + + def flate_encode(self): if SA.FILTER in self: f = self[SA.FILTER] if isinstance(f, ArrayObject): @@ -1044,21 +1103,55 @@ def flateEncode(self): class DecodedStreamObject(StreamObject): + def get_data(self): + return self._data + + def set_data(self, data): + self._data = data + def getData(self): + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) return self._data def setData(self, data): - self._data = data + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) + self.set_data(data) class EncodedStreamObject(StreamObject): def __init__(self): - self.decodedSelf = None + self.decoded_self = None - def getData(self): - if self.decodedSelf: + @property + def decodedSelf(self): + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) + return self.decoded_self + + @decodedSelf.setter + def decodedSelf(self, value): + warnings.warn( + DEPR_MSG.format("decodedSelf", "decoded_self"), + PendingDeprecationWarning, + stacklevel=2, + ) + self.decoded_self = value + + def get_data(self): + if self.decoded_self: # cached version of decoded object - return self.decodedSelf.getData() + return self.decoded_self.get_data() else: # create decoded object decoded = DecodedStreamObject() @@ -1067,10 +1160,10 @@ def getData(self): for key, value in list(self.items()): if key not in (SA.LENGTH, SA.FILTER, SA.DECODE_PARMS): decoded[key] = value - self.decodedSelf = decoded + self.decoded_self = decoded return decoded._data - def setData(self, data): + def set_data(self, data): raise PdfReadError("Creating EncodedStreamObject is not currently supported") @@ -1084,10 +1177,10 @@ def __init__(self, stream, pdf): if isinstance(stream, ArrayObject): data = b_("") for s in stream: - data += b_(s.get_object().getData()) + data += b_(s.get_object().get_data()) stream = BytesIO(b_(data)) else: - stream = BytesIO(b_(stream.getData())) + stream = BytesIO(b_(stream.get_data())) self.__parseContentStream(stream) def __parseContentStream(self, stream): @@ -1223,13 +1316,21 @@ def __init__(self, arr): # must have four points assert len(arr) == 4 # automatically convert arr[x] into NumberObject(arr[x]) if necessary - ArrayObject.__init__(self, [self.ensureIsNumber(x) for x in arr]) + ArrayObject.__init__(self, [self._ensure_is_number(x) for x in arr]) - def ensureIsNumber(self, value): + def _ensure_is_number(self, value): if not isinstance(value, (NumberObject, FloatObject)): value = FloatObject(value) return value + def ensureIsNumber(self, value): + warnings.warn( + "ensureIsNumber will be removed in PyPDF2 2.0.0. ", + PendingDeprecationWarning, + stacklevel=2, + ) + return self._ensure_is_number(value) + def __repr__(self): return "RectangleObject(%s)" % repr(list(self)) @@ -1323,7 +1424,7 @@ def lower_left(self): @lower_left.setter def lower_left(self, value): - self[0], self[1] = [self.ensureIsNumber(x) for x in value] + self[0], self[1] = [self._ensure_is_number(x) for x in value] @property def lower_right(self): @@ -1335,7 +1436,7 @@ def lower_right(self): @lower_right.setter def lower_right(self, value): - self[2], self[1] = [self.ensureIsNumber(x) for x in value] + self[2], self[1] = [self._ensure_is_number(x) for x in value] @property def upper_left(self): @@ -1347,7 +1448,7 @@ def upper_left(self): @upper_left.setter def upper_left(self, value): - self[0], self[3] = [self.ensureIsNumber(x) for x in value] + self[0], self[3] = [self._ensure_is_number(x) for x in value] @property def upper_right(self): @@ -1359,7 +1460,7 @@ def upper_right(self): @upper_right.setter def upper_right(self, value): - self[2], self[3] = [self.ensureIsNumber(x) for x in value] + self[2], self[3] = [self._ensure_is_number(x) for x in value] def getLowerLeft(self): warnings.warn( @@ -1407,7 +1508,7 @@ def setLowerRight(self, value): PendingDeprecationWarning, stacklevel=2, ) - self[2], self[1] = [self.ensureIsNumber(x) for x in value] + self[2], self[1] = [self._ensure_is_number(x) for x in value] def setUpperLeft(self, value): warnings.warn( @@ -1415,7 +1516,7 @@ def setUpperLeft(self, value): PendingDeprecationWarning, stacklevel=2, ) - self[0], self[3] = [self.ensureIsNumber(x) for x in value] + self[0], self[3] = [self._ensure_is_number(x) for x in value] def setUpperRight(self, value): warnings.warn( @@ -1423,7 +1524,7 @@ def setUpperRight(self, value): PendingDeprecationWarning, stacklevel=2, ) - self[2], self[3] = [self.ensureIsNumber(x) for x in value] + self[2], self[3] = [self._ensure_is_number(x) for x in value] @property def width(self): @@ -1517,7 +1618,7 @@ def upperRight(self, value): class Field(TreeObject): """ A class representing a field dictionary. This class is accessed through - :meth:`getFields()` + :meth:`get_fields()` """ def __init__(self, data): @@ -1541,10 +1642,24 @@ def __init__(self, data): pass @property - def fieldType(self): + def field_type(self): """Read-only property accessing the type of this field.""" return self.get("/FT") + @property + def fieldType(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`field_type` instead. + """ + warnings.warn( + "fieldType will be removed in PyPDF2 2.0.0. " + "Use the field_type property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.field_type + @property def parent(self): """Read-only property accessing the parent of this field.""" @@ -1561,19 +1676,47 @@ def name(self): return self.get("/T") @property - def altName(self): + def alternate_name(self): """Read-only property accessing the alternate name of this field.""" return self.get("/TU") @property - def mappingName(self): + def altName(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`alternate_name` instead. + """ + warnings.warn( + "altName will be removed in PyPDF2 2.0.0. " + "Use the alternate_name property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.alternate_name + + @property + def mapping_name(self): """ Read-only property accessing the mapping name of this field. This name is used by PyPDF2 as a key in the dictionary returned by - :meth:`getFields()` + :meth:`get_fields()` """ return self.get("/TM") + @property + def mappingName(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`mapping_name` instead. + """ + warnings.warn( + "mappingName will be removed in PyPDF2 2.0.0. " + "Use the mapping_name property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.mapping_name + @property def flags(self): """ @@ -1591,12 +1734,26 @@ def value(self): return self.get("/V") @property - def defaultValue(self): + def default_value(self): """Read-only property accessing the default value of this field.""" return self.get("/DV") @property - def additionalActions(self): + def defaultValue(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`default_value` instead. + """ + warnings.warn( + "defaultValue will be removed in PyPDF2 2.0.0. " + "Use the default_value property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.default_value + + @property + def additional_actions(self): """ Read-only property accessing the additional actions dictionary. This dictionary defines the field's behavior in response to trigger events. @@ -1604,6 +1761,20 @@ def additionalActions(self): """ self.get("/AA") + @property + def additionalActions(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`additional_actions` instead. + """ + warnings.warn( + "additionalActions will be removed in PyPDF2 2.0.0. " + "Use the additional_actions property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.additional_actions + class Destination(TreeObject): """ @@ -1670,7 +1841,8 @@ def __init__(self, title, page, typ, *args): else: raise PdfReadError("Unknown Destination Type: %r" % typ) - def getDestArray(self): + @property + def dest_array(self): return ArrayObject( [self.raw_get("/Page"), self["/Type"]] + [ @@ -1680,12 +1852,25 @@ def getDestArray(self): ] ) + def getDestArray(self): + """ + .. deprecated:: 1.28.3 + Use :py:attr:`dest_array` instead. + """ + warnings.warn( + "getDestArray will be removed in PyPDF2 2.0.0. " + "Use the dest_array property instead.", + PendingDeprecationWarning, + stacklevel=2, + ) + return self.dest_array + def write_to_stream(self, stream, encryption_key): stream.write(b_("<<\n")) key = NameObject("/D") key.write_to_stream(stream, encryption_key) stream.write(b_(" ")) - value = self.getDestArray() + value = self.dest_array value.write_to_stream(stream, encryption_key) key = NameObject("/S") @@ -1786,7 +1971,7 @@ def write_to_stream(self, stream, encryption_key): key = NameObject("/Dest") key.write_to_stream(stream, encryption_key) stream.write(b_(" ")) - value = self.getDestArray() + value = self.dest_array value.write_to_stream(stream, encryption_key) stream.write(b_("\n")) stream.write(b_(">>")) diff --git a/PyPDF2/merger.py b/PyPDF2/merger.py index 80e70379e..f9f29f442 100644 --- a/PyPDF2/merger.py +++ b/PyPDF2/merger.py @@ -29,7 +29,7 @@ from sys import version_info from PyPDF2._reader import PdfReader -from PyPDF2._utils import _isString, DEPR_MSG, str_ +from PyPDF2._utils import DEPR_MSG, _isString, str_ from PyPDF2._writer import PdfWriter from PyPDF2.constants import PagesAttributes as PA from PyPDF2.generic import * @@ -177,7 +177,7 @@ def merge( outline = [] if import_bookmarks: - outline = reader.get_outlines() + outline = reader.outlines outline = self._trim_outline(reader, outline, pages) if bookmark: @@ -272,7 +272,7 @@ def close(self): usage. """ self.pages = [] - for fo, _pdfr, mine in self.inputs: + for fo, _reader, mine in self.inputs: if mine: fo.close() @@ -639,7 +639,7 @@ def add_bookmark( dest = Destination( NameObject("/" + title + " bookmark"), page_ref, NameObject(fit), *zoom_args ) - dest_array = dest.getDestArray() + dest_array = dest.dest_array action.update( {NameObject("/D"): dest_array, NameObject("/S"): NameObject("/GoTo")} ) @@ -674,7 +674,7 @@ def add_bookmark( bookmark_ref = self.output._add_object(bookmark) parent = parent.get_object() - parent.addChild(bookmark_ref, self.output) + parent.add_child(bookmark_ref, self.output) return bookmark_ref @@ -747,7 +747,7 @@ def add(self, title, pagenum): self.pdf._addObject(bookmark) - self.tree.addChild(bookmark) + self.tree.add_child(bookmark) def removeAll(self): for child in self.tree.children(): diff --git a/PyPDF2/xmp.py b/PyPDF2/xmp.py index ed0d6c931..d66cca8e7 100644 --- a/PyPDF2/xmp.py +++ b/PyPDF2/xmp.py @@ -58,12 +58,12 @@ class XmpInformation(PdfObject): """ An object that represents Adobe XMP metadata. - Usually accessed by :meth:`getXmpMetadata()` + Usually accessed by :py:attr:`xmp_metadata()` """ def __init__(self, stream): self.stream = stream - doc_root = parseString(self.stream.getData()) + doc_root = parseString(self.stream.get_data()) self.rdfRoot = doc_root.getElementsByTagNameNS(RDF_NAMESPACE, "RDF")[0] self.cache = {} @@ -100,6 +100,8 @@ def getElement(self, aboutUri, namespace, name): """ warnings.warn( DEPR_MSG.format("getElement", "get_element"), + PendingDeprecationWarning, + stacklevel=2, ) return self.get_element(aboutUri, namespace, name) @@ -122,6 +124,8 @@ def getNodesInNamespace(self, aboutUri, namespace): """ warnings.warn( DEPR_MSG.format("getNodesInNamespace", "get_nodes_in_namespace"), + PendingDeprecationWarning, + stacklevel=2, ) return self.get_nodes_in_namespace(aboutUri, namespace) @@ -166,7 +170,7 @@ def get(self): if cached: return cached retval = [] - for element in self.getElement("", namespace, name): + for element in self.get_element("", namespace, name): bags = element.getElementsByTagNameNS(RDF_NAMESPACE, "Bag") if len(bags): for bag in bags: diff --git a/Tests/bench.py b/Tests/bench.py index b2416b724..f881bf4c0 100644 --- a/Tests/bench.py +++ b/Tests/bench.py @@ -25,7 +25,7 @@ def page_ops(pdf_path, password): page.scale(2, 2) page.scaleBy(0.5) page.scaleTo(100, 100) - page.compressContentStreams() + page.compress_content_streams() page.extract_text() @@ -44,40 +44,42 @@ def merge(): pdf_forms = os.path.join(RESOURCE_ROOT, "pdflatex-forms.pdf") pdf_pw = os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf") - file_merger = PyPDF2.PdfMerger() + merger = PyPDF2.PdfMerger() # string path: - file_merger.append(pdf_path) - file_merger.append(outline) - file_merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0))) - file_merger.append(pdf_forms) + merger.append(pdf_path) + merger.append(outline) + merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0))) + merger.append(pdf_forms) # Merging an encrypted file - pdfr = PyPDF2.PdfReader(pdf_pw) - pdfr.decrypt("openpassword") - file_merger.append(pdfr) + reader = PyPDF2.PdfReader(pdf_pw) + reader.decrypt("openpassword") + merger.append(reader) # PdfReader object: - file_merger.append(PyPDF2.PdfReader(pdf_path, "rb"), bookmark=True) + merger.append(PyPDF2.PdfReader(pdf_path, "rb"), bookmark=True) # File handle with open(pdf_path, "rb") as fh: - file_merger.append(fh) + merger.append(fh) - bookmark = file_merger.add_bookmark("A bookmark", 0) - file_merger.add_bookmark("deeper", 0, parent=bookmark) - file_merger.add_metadata({"author": "Martin Thoma"}) - file_merger.add_named_destionation("title", 0) - file_merger.set_page_layout("/SinglePage") - file_merger.set_page_mode("/UseThumbs") + bookmark = merger.add_bookmark("A bookmark", 0) + merger.add_bookmark("deeper", 0, parent=bookmark) + merger.add_metadata({"author": "Martin Thoma"}) + merger.add_named_destionation("title", 0) + merger.set_page_layout("/SinglePage") + merger.set_page_mode("/UseThumbs") tmp_path = "dont_commit_merged.pdf" - file_merger.write(tmp_path) - file_merger.close() + merger.write(tmp_path) + merger.close() # Check if bookmarks are correct - pdfr = PyPDF2.PdfReader(tmp_path) - assert [el.title for el in pdfr.get_outlines() if isinstance(el, Destination)] == [ + reader = PyPDF2.PdfReader(tmp_path) + assert [ + el.title for el in reader._get_outlines() if isinstance(el, Destination) + ] == [ "A bookmark", "Foo", "Bar", diff --git a/Tests/test_generic.py b/Tests/test_generic.py index dc9e1a954..6412c8c0c 100644 --- a/Tests/test_generic.py +++ b/Tests/test_generic.py @@ -220,7 +220,7 @@ def test_DictionaryObject_key_is_no_pdfobject(): def test_DictionaryObject_xmp_meta(): do = DictionaryObject({NameObject("/S"): NameObject("/GoTo")}) - assert do.xmpMetadata is None + assert do.xmp_metadata is None def test_DictionaryObject_value_is_no_pdfobject(): diff --git a/Tests/test_merger.py b/Tests/test_merger.py index d405223dd..5d35400aa 100644 --- a/Tests/test_merger.py +++ b/Tests/test_merger.py @@ -17,40 +17,42 @@ def test_merge(): pdf_forms = os.path.join(RESOURCE_ROOT, "pdflatex-forms.pdf") pdf_pw = os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf") - file_merger = PyPDF2.PdfMerger() + merger = PyPDF2.PdfMerger() # string path: - file_merger.append(pdf_path) - file_merger.append(outline) - file_merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0))) - file_merger.append(pdf_forms) + merger.append(pdf_path) + merger.append(outline) + merger.append(pdf_path, pages=PyPDF2.pagerange.PageRange(slice(0, 0))) + merger.append(pdf_forms) # Merging an encrypted file - pdfr = PyPDF2.PdfReader(pdf_pw) - pdfr.decrypt("openpassword") - file_merger.append(pdfr) + reader = PyPDF2.PdfReader(pdf_pw) + reader.decrypt("openpassword") + merger.append(reader) # PdfReader object: - file_merger.append(PyPDF2.PdfReader(pdf_path, "rb"), bookmark=True) + merger.append(PyPDF2.PdfReader(pdf_path, "rb"), bookmark=True) # File handle with open(pdf_path, "rb") as fh: - file_merger.append(fh) + merger.append(fh) - bookmark = file_merger.add_bookmark("A bookmark", 0) - file_merger.add_bookmark("deeper", 0, parent=bookmark) - file_merger.add_metadata({"author": "Martin Thoma"}) - file_merger.add_named_destionation("title", 0) - file_merger.set_page_layout("/SinglePage") - file_merger.set_page_mode("/UseThumbs") + bookmark = merger.add_bookmark("A bookmark", 0) + merger.add_bookmark("deeper", 0, parent=bookmark) + merger.add_metadata({"author": "Martin Thoma"}) + merger.add_named_destionation("title", 0) + merger.set_page_layout("/SinglePage") + merger.set_page_mode("/UseThumbs") tmp_path = "dont_commit_merged.pdf" - file_merger.write(tmp_path) - file_merger.close() + merger.write(tmp_path) + merger.close() # Check if bookmarks are correct - pdfr = PyPDF2.PdfReader(tmp_path) - assert [el.title for el in pdfr.get_outlines() if isinstance(el, Destination)] == [ + reader = PyPDF2.PdfReader(tmp_path) + assert [ + el.title for el in reader._get_outlines() if isinstance(el, Destination) + ] == [ "A bookmark", "Foo", "Bar", diff --git a/Tests/test_page.py b/Tests/test_page.py index d7a86e2f9..89489fad3 100644 --- a/Tests/test_page.py +++ b/Tests/test_page.py @@ -71,7 +71,7 @@ def test_page_operations(pdf_path, password): page.scale(2, 2) page.scaleBy(0.5) page.scaleTo(100, 100) - page.compressContentStreams() + page.compress_content_streams() page.extract_text() @@ -92,7 +92,7 @@ def test_compress_content_streams(pdf_path, password): if password: reader.decrypt(password) for page in reader.pages: - page.compressContentStreams() + page.compress_content_streams() def test_page_properties(): diff --git a/Tests/test_reader.py b/Tests/test_reader.py index ca81f6b50..ed1874307 100644 --- a/Tests/test_reader.py +++ b/Tests/test_reader.py @@ -125,7 +125,7 @@ def test_get_attachments(src): annotobj = annotation.get_object() if annotobj[IA.SUBTYPE] == "/FileAttachment": fileobj = annotobj["/FS"] - attachments[fileobj["/F"]] = fileobj["/EF"]["/F"].getData() + attachments[fileobj["/F"]] = fileobj["/EF"]["/F"].get_data() return attachments @@ -138,7 +138,7 @@ def test_get_attachments(src): ) def test_get_outlines(src, outline_elements): reader = PdfReader(src) - outlines = reader.get_outlines() + outlines = reader._get_outlines() assert len(outlines) == outline_elements @@ -310,16 +310,16 @@ def test_get_form(src, expected, expected_get_fields): for field in fields.values(): # Just access the attributes [ - field.fieldType, + field.field_type, field.parent, field.kids, field.name, field.altName, - field.mappingName, + field.mapping_name, field.flags, field.value, - field.defaultValue, - field.additionalActions, + field.default_value, + field.additional_actions, ] @@ -505,7 +505,7 @@ def test_read_encrypted_without_decryption(): def test_get_destination_age_number(): src = os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf") reader = PdfReader(src) - outlines = reader.get_outlines() + outlines = reader._get_outlines() for outline in outlines: if not isinstance(outline, list): reader.get_destination_page_number(outline) @@ -562,13 +562,13 @@ def test_issue604(strict): if strict: with pytest.raises(PdfReadError) as exc: pdf = PdfReader(f, strict=strict) - bookmarks = pdf.get_outlines() + bookmarks = pdf._get_outlines() if "Unknown Destination" not in exc.value.args[0]: raise Exception("Expected exception not raised") return # bookmarks not correct else: pdf = PdfReader(f, strict=strict) - bookmarks = pdf.get_outlines() + bookmarks = pdf._get_outlines() def getDestPages(x): # print(x) diff --git a/Tests/test_workflows.py b/Tests/test_workflows.py index 8afbf8301..16f2db538 100644 --- a/Tests/test_workflows.py +++ b/Tests/test_workflows.py @@ -59,7 +59,7 @@ def test_PdfReaderJpegImage(): page = reader._get_page(0) x_object = page[PG.RESOURCES]["/XObject"].get_object() - data = x_object["/Im4"].getData() + data = x_object["/Im4"].get_data() # Compare the text of the PDF to a known source assert binascii.hexlify(data).decode() == imagetext, ( diff --git a/docs/user/cropping-and-transforming.md b/docs/user/cropping-and-transforming.md index edd37e2e7..4962f406c 100644 --- a/docs/user/cropping-and-transforming.md +++ b/docs/user/cropping-and-transforming.md @@ -49,7 +49,7 @@ page_box = reader.pages[0] page_base.mergeTransformedPage(page_box, Transformation()) # Write the result back writer = PdfWriter() -writer.addPage(page_base) +writer.add_page(page_base) with open("merged-foo.pdf", "wb") as fp: writer.write(fp) ``` @@ -71,7 +71,7 @@ op = Transformation().rotate(45) page_base.mergeTransformedPage(page_box, op) # Write the result back writer = PdfWriter() -writer.addPage(page_base) +writer.add_page(page_base) with open("merged-foo.pdf", "wb") as fp: writer.write(fp) ``` diff --git a/docs/user/file-size.md b/docs/user/file-size.md index 17a866bcb..7b4b93df8 100644 --- a/docs/user/file-size.md +++ b/docs/user/file-size.md @@ -30,8 +30,8 @@ reader = PyPDF2.PdfReader("example.pdf") writer = PyPDF2.PdfWriter() for page in reader.pages: - page.compressContentStreams() - writer.addPage(page) + page.compress_content_streams() + writer.add_page(page) with open("out.pdf", "wb") as f: writer.write(f) diff --git a/docs/user/reading-pdf-annotations.md b/docs/user/reading-pdf-annotations.md index 91aa4c76b..8c01fe728 100644 --- a/docs/user/reading-pdf-annotations.md +++ b/docs/user/reading-pdf-annotations.md @@ -63,5 +63,5 @@ for page in reader.pages: subtype = annot.get_object()["/Subtype"] if subtype == "/FileAttachment": fileobj = annotobj["/FS"] - attachments[fileobj["/F"]] = fileobj["/EF"]["/F"].getData() + attachments[fileobj["/F"]] = fileobj["/EF"]["/F"].get_data() ```