forked from anshulkhare7/python-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergePDF.py
More file actions
executable file
·82 lines (65 loc) · 2.46 KB
/
mergePDF.py
File metadata and controls
executable file
·82 lines (65 loc) · 2.46 KB
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
#!/usr/bin/env python
from PyPDF2 import PdfFileReader, PdfFileWriter
import pikepdf
def remove_page(path, output, page_to_Remove):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(path)
idx = 1
for page in range(pdf_reader.getNumPages()):
# Add each page to the writer object
if(idx!=page_to_Remove):
print("Adding Page: "+str(idx))
pdf_writer.addPage(pdf_reader.getPage(page))
idx = idx+1
with open(output, 'wb') as out:
pdf_writer.write(out)
def merge_pdfs(paths, output):
pdf_writer = PdfFileWriter()
for path in paths:
pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
# Add each page to the writer object
pdf_writer.addPage(pdf_reader.getPage(page))
# Write out the merged PDF
with open(output, 'wb') as out:
pdf_writer.write(out)
def extract_information(pdf_path):
with open(pdf_path, 'rb') as f:
pdf = PdfFileReader(f)
information = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
# txt = f"""
# Information about {pdf_path}:
# Author: {information.author}
# Creator: {information.creator}
# Producer: {information.producer}
# Subject: {information.subject}
# Title: {information.title}
# Number of pages: {number_of_pages}
# """
print(txt)
return information
def decrypt_pdf(input_path, output_path, password):
with open(input_path, 'rb') as input_file, \
open(output_path, 'wb') as output_file:
reader = PdfFileReader(input_file)
reader.decrypt(password)
writer = PdfFileWriter()
for i in range(reader.getNumPages()):
writer.addPage(reader.getPage(i))
writer.write(output_file)
def decrypt_pdf2(input_path, output_path, password):
with pikepdf.open(input_path, password) as pdf:
num_pages = len(pdf.pages)
print("Total pages:", num_pages)
if __name__ == '__main__':
doc_1 = '/Users/anshul/Downloads/input01.pdf'
doc_2 = '/Users/anshul/Downloads/input02.pdf'
#extract_information('/Users/anshul/Downloads/abc.pdf')
paths = [doc_1, doc_2]
merge_pdfs(paths, output='/Users/anshul/Downloads/output.pdf')
#remove_page(doc_1,doc_2,1)
#encrypted = '/Users/anshul/Downloads/abc.pdf'
#decrypted = '/Users/anshul/Downloads/abc.pdf'
##password = 'password'
#decrypt_pdf(encrypted, decrypted, password)