Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5feba26

Browse files
🎨 Add addWatermark, combinePDF and encryptDecryptPDF
1 parent 002bc7f commit 5feba26

File tree

5 files changed

+234
-9
lines changed

5 files changed

+234
-9
lines changed

‎README.md‎

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ pip install -r requirements.txt
3939

4040
## Contents 📄
4141

42-
- Contents
43-
- [Code Destroyer](#Code-Destroyer)
44-
- [Phone Number and Email Extractor](#Phone-Number-and-Email-Extractor)
45-
- [Search files based on size](#Search-files-based-on-size)
46-
- [Fill gaps in naming](#Fill-gaps-in-naming)
47-
48-
- [License](LICENSE)
42+
- [Code Destroyer](#Code-Destroyer)
43+
- [Extract Phone Number and Email](#Extract-Phone-Number-and-Email)
44+
- [Search files based on size](#Search-files-based-on-size)
45+
- [Fill gaps in naming](#Fill-gaps-in-naming)
46+
- [Combine PDF files](#Combine-PDF-files)
47+
-[Add Watermark to PDF](#Add-Watermark-to-PDF)
48+
- [Encrypt or Decrypt a PDF](#Encrypt-or-Decrypt-a-PDF)
4949

5050
### Code Destroyer
5151

@@ -63,9 +63,9 @@ Enter the full filename (like helloWorld.c) in the prompt that follows.
6363

6464
__Disclaimer: Do not use this to prank on someone's hard-work. You know how frustating that feels.__
6565

66-
### Phone Number and Email Extractor
66+
### Extract Phone Number and Email
6767

68-
Phone Number and Email Extractor takes in the text from your clipboard and saves the Phone Numbers and Email Addresses found in it to .txt files. It searches for Indian Mobile Phone Numbers, Toll-Free Numbers, Telephone Numbers and Emails using Regular Expressions.
68+
Takes in the text from your clipboard and saves the Phone Numbers and Email Addresses found in it to .txt files. It searches for Indian Mobile Phone Numbers, Toll-Free Numbers, Telephone Numbers and Emails using Regular Expressions.
6969

7070
#### Usage:
7171

@@ -100,6 +100,56 @@ python3 fillGap.py
100100
Enter the file prefix, extesion name and taret folder in the prompt that appears. The files would be named in
101101
order, closing gaps, if any.
102102

103+
### Combine PDF files
104+
105+
Combines all the PDFs in the current working directory into
106+
a single PDF. The program also prompts user if they want to include the cover page of all the PDFs that are being merged.
107+
108+
It is recommended to rename files so that they are lexographically in the same order as they are to be combined and put them in the same directory as the script.
109+
The combined PDF would be saved as the name of the first file in the lexographic order prepended with 'combined'.
110+
111+
__Ensure that none of the PDFs are encrypted.__
112+
113+
#### Usage:
114+
115+
```py3
116+
python3 combinePDF.py
117+
```
118+
119+
Choose whether you want to include the cover page of each individual PDF by entering `y` or `n`.
120+
121+
### Add Watermark to PDF
122+
123+
Add watermark to every page of a PDF document.
124+
125+
The watermark file should be a PDF too. If you want to make an image or text as a watermark, put them in a word file and stylize as per you want it to appear as the watermark and then export the file as PDF. This file would be the watermark file.
126+
127+
__Ensure that none of the PDFs are encrypted.__
128+
129+
#### Usage:
130+
131+
```py3
132+
python3 addWatermarkPDF.py
133+
```
134+
135+
Enter the filenames of the PDF to be watermarked and then of the watermark PDF.
136+
137+
### Encrypt or Decrypt a PDF
138+
139+
Encrypt an unencrypted PDF file with a password or decrypt a password-protected PDF and save as an unencrypted file.
140+
141+
#### Usage:
142+
143+
```py3
144+
python3 encryptDecryptPDF.py
145+
```
146+
147+
Choose whether you want to encrypt or decrypt a PDF and then enter the name of the file. You would be prompted to enter the password either to encrypt the PDF or decrypt it, as selected earlier.
148+
149+
## License
150+
151+
[MIT License](LICENSE)
152+
103153
---
104154

105155
File Templates taken from [awesome-bashrc](https://github.com/aashutoshrathi/awesome-bashrc) and [HackerRank-Test-Case-Generator](https://github.com/aashutoshrathi/HackerRank-Test-Case-Generator/).

‎addWatermarkPDF/addWatermarkPDF.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
addWatermarkPDF.py - Add watermark to every page of a PDF document
3+
4+
Usage: run "python3 addWatermarkPDF.py" and enter the filenames of
5+
the PDF to be watermarked and the watermark PDF.
6+
"""
7+
8+
from os import path
9+
import sys
10+
import PyPDF3
11+
12+
# PDF filenames
13+
baseFileName = input('Enter the name of the PDF file to be watermarked:\n')
14+
baseFileName = path.abspath(baseFileName)
15+
watermarkName = input('Enter the name of watermark PDF:\n')
16+
watermarkName = path.abspath(watermarkName)
17+
18+
# Check if entered filenames are valid
19+
if not path.exists(baseFileName) or baseFileName[-4:].lower() != '.pdf':
20+
print('The filename ' + baseFileName + ' is not a PDF.')
21+
sys.exit()
22+
23+
elif not path.exists(watermarkName) or watermarkName[-4:].lower() != '.pdf':
24+
print('The filename ' + watermarkName + ' is not a PDF.')
25+
sys.exit()
26+
27+
else:
28+
print('Adding Watermark...')
29+
30+
baseFile = open(baseFileName, 'rb')
31+
pdfReader = PyPDF3.PdfFileReader(baseFile)
32+
pdfWatermarkReader = PyPDF3.PdfFileReader(open(watermarkName, 'rb'))
33+
pdfWriter = PyPDF3.PdfFileWriter()
34+
35+
# Merge watermark to each page of PDF
36+
for pageNum in range(pdfReader.numPages):
37+
pageObj = pdfReader.getPage(pageNum)
38+
pageObj.mergePage(pdfWatermarkReader.getPage(0))
39+
pdfWriter.addPage(pageObj)
40+
41+
# Save the resulting PDF to a file
42+
markedPdfFile = open('watermarked' + path.basename(baseFileName), 'wb')
43+
pdfWriter.write(markedPdfFile)
44+
baseFile.close()
45+
markedPdfFile.close()
46+
47+
print('Watermarked file saved as watermarked' + path.basename(baseFileName))

‎combinePDF/combinePDF.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
combinePDF.py - Combines all the PDFs in the current working directory into
3+
a single PDF.
4+
5+
Usage: run "python3 combinePDF.py" and choose whether you want to include the
6+
cover page of each individual PDF by entering y or n.
7+
"""
8+
9+
import os
10+
import PyPDF3
11+
12+
# Get all the PDF filenames
13+
pdfFiles = []
14+
for filename in os.listdir('.'):
15+
if filename.endswith('.pdf'):
16+
pdfFiles.append(filename)
17+
pdfFiles.sort()
18+
19+
pdfWriter = PyPDF3.PdfFileWriter()
20+
21+
print('Combining PDFs...')
22+
23+
# Loop through all the PDF files
24+
for filename in pdfFiles:
25+
pdfFileObj = open(filename, 'rb')
26+
pdfReader = PyPDF3.PdfFileReader(pdfFileObj)
27+
28+
# Loop through the pages and add them
29+
start = 0
30+
if pdfReader.numPages > 1:
31+
ans = input('Do you want to include the cover page of ' + filename\
32+
+ ' ? (y/n): ')
33+
start = 0 if ans.lower() == 'y' else 1
34+
for pageNum in range(start, pdfReader.numPages):
35+
pageObj = pdfReader.getPage(pageNum)
36+
pdfWriter.addPage(pageObj)
37+
38+
# Save the resulting PDF to a file
39+
os.makedirs('combinedPDFs', exist_ok = True)
40+
pdfOutput = open(os.path.join('combinedPDFs', 'combined' + pdfFiles[0]), 'wb')
41+
pdfWriter.write(pdfOutput)
42+
pdfOutput.close()
43+
44+
print('Combined PDF saved as ' + 'combined' + pdfFiles[0])
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
encryptDecryptPDF.py - A program that can encrypt an unencrypted PDF file
3+
with a password or decrypt an already password-protected PDF to an unencrypted
4+
PDF.
5+
6+
Usage: run "python3 encryptDecryptPDF.py" and choose whether you want to
7+
encrypt or decrypt a PDF and then enter the name of the file. You would be
8+
prompted to enter the password either to encrypt the PDF or decrypt it, as
9+
selected earlier.
10+
"""
11+
12+
from os import path
13+
import sys
14+
import PyPDF3
15+
16+
# Encrypt
17+
def encrypt():
18+
pdfName = input('Enter the name of PDF file to encrypt:\n')
19+
pdfName = path.abspath(pdfName)
20+
21+
# Check if entered filename is valid
22+
if not path.exists(pdfName) or pdfName[-4:].lower() != '.pdf':
23+
print('The filename ' + pdfName + ' is not a PDF.')
24+
sys.exit()
25+
26+
pdfFile = open(pdfName, 'rb')
27+
pdfReader = PyPDF3.PdfFileReader(pdfFile)
28+
pdfWriter = PyPDF3.PdfFileWriter()
29+
30+
# Loop through the pages and add them to pdfWriter
31+
for pageNum in range(pdfReader.numPages):
32+
pdfWriter.addPage(pdfReader.getPage(pageNum))
33+
34+
# Password for encryption
35+
password = input('Enter a password to encrypt the PDF: \n')
36+
pdfWriter.encrypt(password)
37+
38+
# Save the resulting PDF to a file
39+
encryptedPdf = open('encrypted' + path.basename(pdfName), 'wb')
40+
pdfWriter.write(encryptedPdf)
41+
encryptedPdf.close()
42+
print('File encrypted and saved as encrypted' + path.basename(pdfName))
43+
44+
# Decrypt
45+
def decrypt():
46+
pdfName = input('Enter the name of PDF file to decrypt:\n')
47+
pdfName = path.abspath(pdfName)
48+
49+
# Check if entered filename is valid
50+
if not path.exists(pdfName) or pdfName[-4:].lower() != '.pdf':
51+
print('The filename ' + pdfName + ' is not a PDF.')
52+
sys.exit()
53+
54+
pdfFile = open(pdfName, 'rb')
55+
pdfReader = PyPDF3.PdfFileReader(pdfFile)
56+
57+
# Password for decryption
58+
password = input('Enter the password to decrypt the PDF: \n')
59+
pdfReader.decrypt(password)
60+
pdfWriter = PyPDF3.PdfFileWriter()
61+
62+
# Loop through the pages and add them to pdfWriter
63+
for pageNum in range(pdfReader.numPages):
64+
pdfWriter.addPage(pdfReader.getPage(pageNum))
65+
66+
# Save the resulting PDF to a file
67+
decryptedPdf = open('decrypted' + path.basename(pdfName), 'wb')
68+
pdfWriter.write(decryptedPdf)
69+
decryptedPdf.close()
70+
print('File decrypted and saved as decrypted' + path.basename(pdfName))
71+
72+
print('Enter the task to perform (1 or 2)\n1. Encrypt a PDF\n2. Decrypt a PDF')
73+
response = int(input())
74+
75+
if response == 1:
76+
encrypt()
77+
78+
elif response ==2:
79+
decrypt()
80+
81+
else:
82+
print('You entered a wrong response.')
83+
sys.exit()

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pyperclip==1.7.0
2+
PyPDF3==1.0.1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /