Skip to content

Commit

Permalink
STY: Modify error messages in PdfWriter (#2902)
Browse files Browse the repository at this point in the history
Also some documentation tweaks in PdfWriter.
  • Loading branch information
j-t-1 authored Oct 14, 2024
1 parent f5c3cf2 commit 482a484
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def set_need_appearances_writer(self, state: bool = True) -> None:
Returns:
None
"""
# See 12.7.2 and 7.7.2 for more information:
# See §12.7.2 and §7.7.2 for more information:
# https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
try:
# get the AcroForm tree
Expand Down Expand Up @@ -716,7 +716,7 @@ def add_js(self, javascript: str) -> None:
Add JavaScript which will launch upon opening this PDF.
Args:
javascript: Your Javascript.
javascript: Your JavaScript.
>>> output.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# Example: This will launch the print window when the PDF is opened.
Expand All @@ -741,7 +741,7 @@ def add_js(self, javascript: str) -> None:
NameObject("/JS"): TextStringObject(f"{javascript}"),
}
)
# We need a name for parameterized javascript in the pdf file,
# We need a name for parameterized JavaScript in the PDF file,
# but it can be anything.
js_list.append(create_string_object(str(uuid.uuid4())))
js_list.append(self._add_object(js))
Expand Down Expand Up @@ -1605,7 +1605,7 @@ def compress_identical_objects(
remove_orphans: bool = True,
) -> None:
"""
Parse the PDF file and merge objects that have same hash.
Parse the PDF file and merge objects that have the same hash.
This will make objects common to multiple pages.
Recommended to be used just before writing output.
Expand Down Expand Up @@ -3132,7 +3132,7 @@ def set_page_label(
Page indexes must be given starting from 0.
Labels must have a style, a prefix or both.
If a range is not assigned any page label a decimal label starting from 1 is applied.
If a range is not assigned any page label, a decimal label starting from 1 is applied.
Args:
page_index_from: page index of the beginning of the range starting from 0
Expand All @@ -3155,17 +3155,17 @@ def set_page_label(
Default value: 1.
"""
if style is None and prefix is None:
raise ValueError("at least one between style and prefix must be given")
raise ValueError("At least one of style and prefix must be given")
if page_index_from < 0:
raise ValueError("page_index_from must be equal or greater then 0")
raise ValueError("page_index_from must be greater or equal than 0")
if page_index_to < page_index_from:
raise ValueError(
"page_index_to must be equal or greater then page_index_from"
"page_index_to must be greater or equal than page_index_from"
)
if page_index_to >= len(self.pages):
raise ValueError("page_index_to exceeds number of pages")
if start is not None and start != 0 and start < 1:
raise ValueError("if given, start must be equal or greater than one")
raise ValueError("If given, start must be greater or equal than one")

self._set_page_label(page_index_from, page_index_to, style, prefix, start)

Expand Down Expand Up @@ -3241,8 +3241,8 @@ def _pdf_objectify(obj: Union[Dict[str, Any], str, int, List[Any]]) -> PdfObject
to_add = DictionaryObject()
for key, value in obj.items():
name_key = NameObject(key)
casted_value = _pdf_objectify(value)
to_add[name_key] = casted_value
cast_value = _pdf_objectify(value)
to_add[name_key] = cast_value
return to_add
elif isinstance(obj, str):
if obj.startswith("/"):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,21 +1187,21 @@ def test_set_page_label(pdf_file_path):
writer = PdfWriter()
writer.clone_document_from_reader(reader)
with pytest.raises(
ValueError, match="at least one between style and prefix must be given"
ValueError, match="At least one of style and prefix must be given"
):
writer.set_page_label(0, 5, start=2)
with pytest.raises(
ValueError, match="page_index_from must be equal or greater then 0"
ValueError, match="page_index_from must be greater or equal than 0"
):
writer.set_page_label(-1, 5, "/r")
with pytest.raises(
ValueError, match="page_index_to must be equal or greater then page_index_from"
ValueError, match="page_index_to must be greater or equal than page_index_from"
):
writer.set_page_label(5, 0, "/r")
with pytest.raises(ValueError, match="page_index_to exceeds number of pages"):
writer.set_page_label(0, 19, "/r")
with pytest.raises(
ValueError, match="if given, start must be equal or greater than one"
ValueError, match="If given, start must be greater or equal than one"
):
writer.set_page_label(0, 5, "/r", start=-1)

Expand Down

0 comments on commit 482a484

Please sign in to comment.