Skip to content

Commit ac8e2a6

Browse files
authored
Create pdf_tools.py
1 parent a4b7a62 commit ac8e2a6

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

pdf_tools/pdf_tools.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Oct 27 13:49:16 2021
4+
5+
@author: gattsu997
6+
"""
7+
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
11+
12+
13+
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger
14+
from pathlib import Path
15+
16+
print("this script can print, edit, modify, rotate and add passwords to PDF documents if used properly")
17+
18+
pdf_path = (Path.home()/ "myfile.pdf" ) //rename/edit address if different
19+
pdf = PdfFileReader(str(pdf_path))
20+
21+
22+
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)
25+
print("number of pages:", pdf.getNumPages())
26+
print("more details:",pdf.documentInfo)
27+
28+
29+
print("to extract pages type OUTPUT")
30+
if input()=="OUTPUT":
31+
print("do you want the entire text or a specific page? (FULL/PAGE")
32+
if input()=="FULL":
33+
for page in pdf.pages:
34+
print("text will be saved in a txt file")
35+
with open('fulltext.txt', 'w') as f:
36+
f.write(page.extractText())
37+
if input()=="PAGE":
38+
print("which page do you want?")
39+
n=int(input())
40+
page=pdf.getPage(n)
41+
print(page.extractText())
42+
43+
44+
print("to make a modified pdf type PDF")
45+
if input()=="PDF":
46+
input_pdf = PdfFileReader(str(pdf_path))
47+
while input()!="NO":
48+
print("which page do you want to add to new PDF?")
49+
pg=input_pdf.getPage(int(input()))
50+
pdf_writer = PdfFileWriter()
51+
pdf_writer.addPage(pg)
52+
with Path("sliced.pdf").open(mode="wb") as output_file:
53+
pdf_writer.write(output_file)
54+
print("add more pages (YES/NO)")
55+
56+
57+
print("if you want to concatenate two PDF docs, type ADD")
58+
if input()=="ADD":
59+
BASE_PATH = (Path.home())
60+
print("name the two files eg file1.pdf, file2.pdf one by one")
61+
m=input()
62+
n=input()
63+
pdf_paths = [BASE_PATH / m, BASE_PATH / n]
64+
pdf_merger = PdfFileMerger()
65+
for path in pdf_paths:
66+
pdf_merger.append(str(path))
67+
output_path = Path.home() / "concatenated.pdf"
68+
with output_path.open(mode="wb") as output_file:
69+
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":
74+
pdf_reader = PdfFileReader(str(pdf_path))
75+
pdf_writer = PdfFileWriter()
76+
for page in pdf_reader.pages:
77+
rotated_page = page.rotateClockwise(90)
78+
pdf_writer.addPage(rotated_page)
79+
if input=="ACW":
80+
pdf_reader = PdfFileReader(str(pdf_path))
81+
pdf_writer = PdfFileWriter()
82+
for page in pdf_reader.pages:
83+
rotated_page = page.rotateCounterClockwise(90)
84+
pdf_writer.addPage(rotated_page)
85+
86+
87+
print("if you want to add password to your file type PWD")
88+
if input()=="PWD":
89+
pdf_reader = PdfFileReader(str(pdf_path))
90+
pdf_writer = PdfFileWriter()
91+
pdf_writer.appendPagesFromReader(pdf_reader)
92+
print("enter password now")
93+
p=input()
94+
pdf_writer.encrypt(user_pwd=p)
95+
output_path = Path.home() / "newsletter_protected.pdf"
96+
with output_path.open(mode="wb") as output_file:
97+
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)