-
Notifications
You must be signed in to change notification settings - Fork 26
/
Project2: Playing with PDFs
69 lines (52 loc) · 1.49 KB
/
Project2: Playing with PDFs
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
# import useful modules
from PyPDF2 import PdfFileWriter,PdfFileReader
# merge pdfs together.
#step->
# open your first pdf
# open your second pdf
#for each page, copy it to third pdf
# open a third pdf
write_obj=PdfFileWriter()
pdf_list=["D:\\Day1 folder\\pdf1.pdf","D:\\Day1 folder\\pdf2.pdf" ]
for i in pdf_list:
red_obj= PdfFileReader(i)
pages=red_obj.getNumPages()
#print(pages)
for j in range(pages):
p=red_obj.getPage(j)
write_obj.addPage(p)
pdf_file=open("D:\\Day1 folder\\pdf1+pdf2.pdf",'wb')
write_obj.write(pdf_file)
# encrypt a pdf
write_obj=PdfFileWriter()
pdf_list=["D:\\Day1 folder\\pdf1.pdf","D:\\Day1 folder\\pdf2.pdf" ]
for i in pdf_list:
red_obj= PdfFileReader(i)
pages=red_obj.getNumPages()
#print(pages)
for j in range(pages):
p=red_obj.getPage(j)
write_obj.addPage(p)
write_obj.encrypt('sweta23','isb2020',True)
pdf_file=open("D:\\Day1 folder\\pdf1+pdf2.pdf",'wb')
write_obj.write(pdf_file)
write_obj.encrypt('sweta23','isb2020',True)
#add watermark to a pdf
#steps->
#read the pdf
#read the watermark
# create a new pdf
#for each page in pdf, merge watermark with it and
#add it to the new pdf.
pdf=PdfFileReader("D:\\Day1 folder\\pdf1.pdf")
watermark=PdfFileReader("D:\\Day1 folder\\watermark.pdf")
page_w=watermark.getPage(0)
new_pdf=PdfFileWriter()
pages=pdf.getNumPages()
for i in range(pages):
page=pdf.getPage(i)
page.mergePage(page_w)
new_pdf.addPage(page)
pdf_fl=open("D:\\Day1 folder\\pdf+watermark.pdf",'wb')
new_pdf.write(pdf_fl)
pdf_fl.close()