Skip to content

Commit a4d3bc7

Browse files
authored
Merge pull request #315 from Tejas1510/mergepdf
Added the final code for mergepdf resolving errors asked by mentor
2 parents 0517c73 + 94fb003 commit a4d3bc7

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

Python/mergetwopdf/MergedFiles.pdf

27.8 KB
Binary file not shown.

Python/mergetwopdf/Readme.MD

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Merge two pdf Files
2+
3+
### Third Party Libraries Required :
4+
1.PyPDF2
5+
6+
### How to install above Library
7+
```
8+
import PyPDF2
9+
```
10+
11+
### How to use it :
12+
1. Download or clone the repository
13+
2. Install Required Libraries
14+
3. Run merge.py
15+
4. you will be asked to give the path of two pdf files
16+
5. you will get the merged pdf in the same directory
17+
18+
### Output
19+
I have added the merged pdf file named as MergedFiles.pdf which is formed from pdf1.pdf pdf2.pdf .
20+
You can check the final output in MergedFiles.pdf File
21+
22+
![built with love](https://forthebadge.com/images/badges/built-with-love.svg)
23+
24+
Check out my Github profile [Tejas1510!](https://github.com/Tejas1510)

Python/mergetwopdf/merge.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import PyPDF2
2+
3+
# Open the pdf file
4+
file1=input("Enter the path of pdf1 : ")
5+
file2=input("Enter the path of pdf2 : ")
6+
pdf1 = open(file1, 'rb')
7+
pdf2 = open(file2, 'rb')
8+
9+
# Read the files
10+
reader1 = PyPDF2.PdfFileReader(pdf1)
11+
reader2 = PyPDF2.PdfFileReader(pdf2)
12+
13+
# Create a new PdfFileWriter object which represents a blank PDF document
14+
writer = PyPDF2.PdfFileWriter()
15+
16+
# Loop through all the pagenumbers for the first document
17+
for i in range(reader1.numPages):
18+
pages = reader1.getPage(i)
19+
writer.addPage(pages)
20+
21+
# Loop through all the pagenumbers for the second document
22+
for i in range(reader2.numPages):
23+
pages = reader2.getPage(i)
24+
writer.addPage(pages)
25+
26+
# Now that you have copied all the pages in both the documents, write them into the a new document
27+
mergedfile = open('MergedFiles.pdf', 'wb')
28+
writer.write(mergedfile)
29+
30+
# Close all the files - Created as well as opened
31+
mergedfile.close()
32+
pdf1.close()
33+
pdf2.close()

Python/mergetwopdf/pdf1.pdf

14.6 KB
Binary file not shown.

Python/mergetwopdf/pdf2.pdf

14.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)