Skip to content

Commit 82acec7

Browse files
committed
Added PDF-Tools Script
1 parent ce7e971 commit 82acec7

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

PDF-Tools/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# PDF-Tools: Multiple functionalities involving PDFs
2+
3+
- Merge PDFs
4+
- Split PDFs
5+
- Add watermark
6+
- Rotate clockwise and counterclocwise
7+
- Encrypt PDFs
8+
9+
## Why is this tool required?
10+
11+
- The various functionalities are distributed across multiple websites online. Hence it is tedious to get the work done.
12+
- Many websites put a limit to the no./size of PDFs you can work on.
13+
- Hence an offline solution to the issue.
14+
15+
## Modules
16+
17+
- PyPDF2
18+
- OS
19+
20+
## Setup instructions
21+
22+
- Clone the repository into your local machine.
23+
- Run: pip3 install -r requirements.txt
24+
- Enter the choice in the console Menu.
25+
- Add the path of PDFs and you are done.
26+
27+
## Author
28+
29+
- Lakshit Farswan
30+
31+
## Contribute
32+
33+
- Extend the project by adding more functionalities.

PDF-Tools/init.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO Convert it into a Python package

PDF-Tools/main.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import os
2+
from PyPDF2 import PdfFileReader, PdfFileWriter
3+
4+
5+
def merge_pdfs():
6+
''' Merge multiple PDF's into one combined PDF '''
7+
8+
input_paths = input(r"Enter comma separated list of paths to the PDFs ")
9+
paths = input_paths.split(',')
10+
pdf_file_writer = PdfFileWriter()
11+
12+
# Pick each pdf one by one and combined to one single pdf
13+
for path in paths:
14+
pdf_file_reader = PdfFileReader(path)
15+
for page in range(pdf_file_reader.getNumPages()):
16+
pdf_file_writer.addPage(pdf_file_reader.getPage(page))
17+
18+
# Output the merged pdf
19+
with open('merged.pdf', 'wb') as out:
20+
pdf_file_writer.write(out)
21+
22+
23+
def split_pdfs():
24+
'''Split PDF to multiple PDF's of 1 Page each'''
25+
26+
input_pdf = input(r"Enter I/P PDF path ")
27+
pdf = PdfFileReader(input_pdf)
28+
for page in range(pdf.getNumPages()):
29+
pdf_file_writer = PdfFileWriter()
30+
pdf_file_writer.addPage(pdf.getPage(page))
31+
32+
# Append page num to each new pdf
33+
output = 'split{page}.pdf'.format(page=page)
34+
with open(output, 'wb') as output_pdf:
35+
pdf_file_writer.write(output_pdf)
36+
37+
38+
def add_watermark():
39+
''' Adds watermark to given PDF.
40+
Note: The watermark PDF should be a image with transparent background '''
41+
42+
input_pdf = input(r"Enter I/P PDF path ")
43+
watermark = input(r"Enter watermark PDF path ")
44+
watermark_obj = PdfFileReader(watermark)
45+
watermark_page = watermark_obj.getPage(0)
46+
47+
pdf_file_reader = PdfFileReader(input_pdf)
48+
pdf_file_writer = PdfFileWriter()
49+
50+
# Watermark all the pages
51+
for page_num in range(pdf_file_reader.getNumPages()):
52+
page = pdf_file_reader.getPage(page_num)
53+
page.mergePage(watermark_page)
54+
pdf_file_writer.addPage(page)
55+
56+
with open('watermarked-pdf.pdf', 'wb') as out:
57+
pdf_file_writer.write(out)
58+
59+
60+
def add_encryption():
61+
''' Encrypts the given PDF with the provided password '''
62+
63+
input_pdf = input(r"Enter I/P PDF path ")
64+
password = input(r"Enter password ")
65+
pdf_file_writer = PdfFileWriter()
66+
pdf_file_reader = PdfFileReader(input_pdf)
67+
68+
for page_num in range(pdf_file_reader.getNumPages()):
69+
pdf_file_writer.addPage(pdf_file_reader.getPage(page_num))
70+
# Encrypt using the password
71+
pdf_file_writer.encrypt(user_pwd=password, owner_pwd=None,
72+
use_128bit=True)
73+
74+
with open('encrypted.pdf', 'wb') as fh:
75+
pdf_file_writer.write(fh)
76+
77+
78+
def rotate_pages():
79+
'''Rotate the given PDF left or right by 90 degrees.'''
80+
81+
input_pdf = input(r"Enter I/P PDF path ")
82+
pdf_file_writer = PdfFileWriter()
83+
pdf_file_reader = PdfFileReader(input_pdf)
84+
orient = input("Specify orientation: clockwise or counterclockwise ")
85+
86+
# Rotate each page one by one accordingly
87+
if(orient == "clockwise"):
88+
for page_num in range(pdf_file_reader.getNumPages()):
89+
rot_page = pdf_file_reader.getPage(page_num).rotateClockwise(90)
90+
pdf_file_writer.addPage(rot_page)
91+
elif(orient == "counterclockwise"):
92+
for page_num in range(pdf_file_reader.getNumPages()):
93+
rot_page = pdf_file_reader.getPage(
94+
page_num).rotateCounterClockwise(90)
95+
pdf_file_writer.addPage(rot_page)
96+
97+
with open('rotated.pdf', 'wb') as fh:
98+
pdf_file_writer.write(fh)
99+
100+
101+
def menu():
102+
'''Menu for the various functionalities offered'''
103+
104+
# Change Current working directory to where the script is located.
105+
# This is done to enable use of relative paths from base folder.
106+
abspath = os.path.abspath(__file__)
107+
dname = os.path.dirname(abspath)
108+
os.chdir(dname)
109+
110+
print("\n Welcome to PDF-Tools \n Store the PDF's in the folder of the script \n Choose from the given options\n")
111+
print(" 1.Merge PDF\n 2.Split PDF\n 3.Rotate PDF\n 4.Add Watermark\n 5.Encrypt PDF\n")
112+
# Call the necessary function according to the choice provided by the user
113+
z = int(input())
114+
if(z == 1):
115+
merge_pdfs()
116+
elif(z == 2):
117+
split_pdfs()
118+
elif(z == 3):
119+
rotate_pages()
120+
elif(z == 4):
121+
add_watermark()
122+
elif(z == 5):
123+
add_encryption()
124+
else:
125+
print("Please select valid choice\n")
126+
menu()
127+
128+
129+
if __name__ == '__main__':
130+
menu()

PDF-Tools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pypdf2

PDF-Tools/sample1.pdf

3.79 MB
Binary file not shown.

PDF-Tools/sample2.pdf

143 KB
Binary file not shown.

0 commit comments

Comments
 (0)