Skip to content

Commit 1f79092

Browse files
authored
Update pdf_tools.py
1 parent 5e175bf commit 1f79092

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed

pdf_tools/pdf_tools.py

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,112 +5,86 @@
55
@author: gattsu997
66
"""
77

8-
#python script to work with pdf docs
9-
#IMPORTANT:: make sure to install PyPDF2 package
10-
#IMPORTANT::keep your pdf file in the SAME file or add path/rename MANUALLY
8+
# python script to work with pdf docs
9+
# IMPORTANT:: make sure to install PyPDF2 package
10+
# IMPORTANT::keep your pdf file in the SAME file or add path/rename MANUALLY
1111

1212

1313
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger
1414
from pathlib import Path
1515

16-
print("this script can print, edit, modify, rotate and add passwords to PDF documents if used properly")
16+
print("this script can print, edit, modify, rotate and add passwords to PDF doc
17+
uments if used properly")
1718

18-
pdf_path = (Path.home()/ "myfile.pdf" ) //rename/edit address if different
19+
pdf_path = (Path.home() / "myfile.pdf") // rename / edit address if different
1920
pdf = PdfFileReader(str(pdf_path))
2021

2122

2223
print("to get info on your file type INFO")
23-
if input()=="INFO":
24-
print("documeng title", pdf.documentInfo.title,"\n", "from page 0 to",int(pdf.documentInfo.title)-1)
24+
if input() == "INFO":
25+
print("documeng title", pdf.documentInfo.title, "\n", "from page 0 to", int
26+
(pdf.documentInfo.title)-1)
2527
print("number of pages:", pdf.getNumPages())
26-
print("more details:",pdf.documentInfo)
27-
28-
28+
print("more details:", pdf.documentInfo)
2929
print("to extract pages type OUTPUT")
30-
if input()=="OUTPUT":
30+
if input() == "OUTPUT":
3131
print("do you want the entire text or a specific page? (FULL/PAGE")
32-
if input()=="FULL":
32+
if input() == "FULL":
3333
for page in pdf.pages:
3434
print("text will be saved in a txt file")
3535
with open('fulltext.txt', 'w') as f:
3636
f.write(page.extractText())
37-
if input()=="PAGE":
37+
if input() = ="PAGE":
3838
print("which page do you want?")
39-
n=int(input())
40-
page=pdf.getPage(n)
39+
n = int(input())
40+
page = pdf.getPage(n)
4141
print(page.extractText())
42-
43-
4442
print("to make a modified pdf type PDF")
45-
if input()=="PDF":
43+
if input() == "PDF":
4644
input_pdf = PdfFileReader(str(pdf_path))
47-
while input()!="NO":
45+
while input() != "NO":
4846
print("which page do you want to add to new PDF?")
49-
pg=input_pdf.getPage(int(input()))
47+
pg = input_pdf.getPage(int(input()))
5048
pdf_writer = PdfFileWriter()
5149
pdf_writer.addPage(pg)
5250
with Path("sliced.pdf").open(mode="wb") as output_file:
5351
pdf_writer.write(output_file)
5452
print("add more pages (YES/NO)")
55-
56-
5753
print("if you want to concatenate two PDF docs, type ADD")
58-
if input()=="ADD":
54+
if input() == "ADD":
5955
BASE_PATH = (Path.home())
6056
print("name the two files eg file1.pdf, file2.pdf one by one")
61-
m=input()
62-
n=input()
57+
m = input()
58+
n = input()
6359
pdf_paths = [BASE_PATH / m, BASE_PATH / n]
6460
pdf_merger = PdfFileMerger()
6561
for path in pdf_paths:
6662
pdf_merger.append(str(path))
6763
output_path = Path.home() / "concatenated.pdf"
6864
with output_path.open(mode="wb") as output_file:
6965
pdf_merger.write(output_file)
70-
71-
72-
print("if you want to rotate all pages clockwise write CW or write ACW for anticlockwise")
73-
if input()=="CW":
66+
print("if you want to rotate all pages clockwise
67+
write CW or write ACW for anticlockwise")
68+
if input() == "CW":
7469
pdf_reader = PdfFileReader(str(pdf_path))
7570
pdf_writer = PdfFileWriter()
7671
for page in pdf_reader.pages:
7772
rotated_page = page.rotateClockwise(90)
7873
pdf_writer.addPage(rotated_page)
79-
if input=="ACW":
74+
if input == "ACW":
8075
pdf_reader = PdfFileReader(str(pdf_path))
8176
pdf_writer = PdfFileWriter()
8277
for page in pdf_reader.pages:
8378
rotated_page = page.rotateCounterClockwise(90)
8479
pdf_writer.addPage(rotated_page)
85-
86-
8780
print("if you want to add password to your file type PWD")
88-
if input()=="PWD":
81+
if input() == "PWD":
8982
pdf_reader = PdfFileReader(str(pdf_path))
9083
pdf_writer = PdfFileWriter()
9184
pdf_writer.appendPagesFromReader(pdf_reader)
9285
print("enter password now")
93-
p=input()
86+
p = input()
9487
pdf_writer.encrypt(user_pwd=p)
9588
output_path = Path.home() / "newsletter_protected.pdf"
9689
with output_path.open(mode="wb") as output_file:
9790
pdf_writer.write(output_file)
98-
99-
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
115-
116-

0 commit comments

Comments
 (0)