-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf-editor.py
97 lines (73 loc) · 2.47 KB
/
pdf-editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
pdf_editor.py
A simple script to edit the title of a PDF file.
This script provides functionality to modify the title of a PDF document and save the modified PDF to a new file.
Usage:
python pdf_editor.py <input_pdf> <output_pdf> <new_title>
Example:
python pdf_editor.py input.pdf output.pdf "New Title"
Dependencies:
- PyPDF2 (https://pythonhosted.org/PyPDF2/)
Author:
Xandra Campo
Date:
March 19, 2024
"""
import sys
from PyPDF2 import PdfReader, PdfWriter
def edit_pdf_title(input_pdf, output_pdf, new_title):
"""
Edits the title of a PDF file and saves the modified PDF to a new file.
Parameters
----------
input_pdf : str
Path to the input PDF file.
output_pdf : str
Path to save the modified PDF file.
new_title : str
New title to set for the PDF document.
Returns
-------
None
Raises
------
FileNotFoundError
If the input PDF file does not exist.
PermissionError
If there is a permission issue while accessing the input or output files.
PyPDF2.utils.PdfReadError
If the input PDF file is invalid or corrupted.
"""
with open(input_pdf, 'rb') as file:
pdf_reader = PdfReader(file)
pdf_writer = PdfWriter()
# Copy all pages from the input PDF to the output PDF
for page in pdf_reader.pages:
pdf_writer.add_page(page)
# Add or modify the document information dictionary with the new title
pdf_writer.add_metadata({'/Title': new_title})
# Write the modified PDF to the output file
with open(output_pdf, 'wb') as output_file:
pdf_writer.write(output_file)
if __name__ == "__main__":
"""
Main block to execute the script from the command line.
Usage
-----
python pdf-editor.py <input_pdf> <output_pdf> <new_title>
Arguments
---------
input_pdf : str
Path to the input PDF file.
output_pdf : str
Path to save the modified PDF file.
new_title : str
New title to set for the PDF document.
Example
-------
python pdf-editor.py path-to-pdf-file-1.pdf path-to-pdf-file-2.pdf new-title
"""
if len(sys.argv) != 4:
print("Usage: python pdf-editor.py <input_pdf> <output_pdf> <new_title>")
sys.exit(1)
edit_pdf_title(input_pdf=sys.argv[1], output_pdf=sys.argv[2], new_title=sys.argv[3])