-
I have two pdfs I want to combine them into single pdf including their outlines. I can do this using other tools but i want to specifically do it using pypdf2 import PyPDF2
# Open the files that have to be merged one by one
pdf1File = open('FirstInputFile.pdf', 'rb')
pdf2File = open('SecondInputFile.pdf', 'rb')
# Read the files that you have opened
pdf1Reader = PyPDF2.PdfFileReader(pdf1File)
pdf2Reader = PyPDF2.PdfFileReader(pdf2File)
# Create a new PdfFileWriter object which represents a blank PDF document
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the pagenumbers for the first document
for pageNum in range(pdf1Reader.numPages):
pageObj = pdf1Reader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# Loop through all the pagenumbers for the second document
for pageNum in range(pdf2Reader.numPages):
pageObj = pdf2Reader.getPage(pageNum)
pdfWriter.addPage(pageObj)
#GET THE OUTLINES
outline1 = pdf1Reader.outlines
outline2 = pdf2Reader.outlines
total_outlines = outline1+outline2
#ADDING THE OUTLINE TO pdfWriter
pdfWriter.addBookmarkDestination(total_outlines)
# Now that you have copied all the pages in both the documents, write them into the a new document
pdfOutputFile = open('MergedFiles.pdf', 'wb')
pdfWriter.write(pdfOutputFile)
# Close all the files - Created as well as opened
pdfOutputFile.close()
pdf1File.close()
pdf2File.close() Here I am trying to add outlines using #GET THE OUTLINES
outline1 = pdf1Reader.outlines
outline2 = pdf2Reader.outlines
total_outlines = outline1+outline2'
#ADDING THE OUTLINE TO pdfWriter
pdfWriter.addBookmarkDestination(total_outlines) But it gives error
What is the right way to get this done? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
so do I,I realize that the official doc url here may out of date |
Beta Was this translation helpful? Give feedback.
-
What exactly in the docs is out of date? Do you want to make a PR to fix it? |
Beta Was this translation helpful? Give feedback.
-
The docs are now at https://pypdf2.readthedocs.io/en/latest/modules/PdfWriter.html |
Beta Was this translation helpful? Give feedback.
-
See #1156 |
Beta Was this translation helpful? Give feedback.
-
In my humble opinion, this post more appropriately belongs on a forum such as StackOverflow rather than an issue on GitHub. Nonetheless, the functionality the poster is seeking is best accomplished using the from PyPDF2 import PdfReader, PdfMerger
doc1 = PdfReader("'first.pdf')
doc2 = PdfReader("second.pdf")
merger = PdfMerger()
merger.append(doc1, import_outline=True)
merger.append(doc2, import_outline=True)
merger.write("first_then_second.pdf") Verify the output by opening the new document in your favorite commercial PDF rendering software. Or, you can inspect the results directly via PyPDF2: print(merger.outline) or combined_doc = PdfReader("first_then_second.pdf")
print(combined_doc.outline) |
Beta Was this translation helpful? Give feedback.
In my humble opinion, this post more appropriately belongs on a forum such as StackOverflow rather than an issue on GitHub.
Nonetheless, the functionality the poster is seeking is best accomplished using the
PdfMerger
class. Using the updated nomenclature (#1156)Verify the output by opening the new document in your favorite commercial PDF rendering software. Or, you can inspect the results directly via PyPDF2:
or