Python pypdf2:如何将大纲转换为输出pdf
我有两个PDF 我想把它们合并成一个pdf,包括它们的大纲 我可以使用其他工具来实现这一点,但我想特别使用pypdf2Python pypdf2:如何将大纲转换为输出pdf,python,pypdf2,Python,Pypdf2,我有两个PDF 我想把它们合并成一个pdf,包括它们的大纲 我可以使用其他工具来实现这一点,但我想特别使用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
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()
在这里,我尝试使用
#GET THE OUTLINES
outline1 = pdf1Reader.outlines
outline2 = pdf2Reader.outlines
total_outlines = outline1+outline2'
#ADDING THE OUTLINE TO pdfWriter
pdfWriter.addBookmarkDestination(total_outlines)
但这会产生错误
Traceback (most recent call last):
File "test100.py", line 15, in <module>
output.addBookmarkDestination(reader1.outlines+reader2.outlines)
File "/usr/lib/python3.7/site-packages/PyPDF2/pdf.py", line 661, in addBookmarkDestination
parent.addChild(destRef, self)
File "/usr/lib/python3.7/site-packages/PyPDF2/generic.py", line 666, in addChild
childObj = child.getObject()
File "/usr/lib/python3.7/site-packages/PyPDF2/generic.py", line 178, in getObject
return self.pdf.getObject(self).getObject()
AttributeError: 'list' object has no attribute 'getObject'
回溯(最近一次呼叫最后一次):
文件“test100.py”,第15行,在
output.addBookmarkDestination(reader1.outlines+reader2.outlines)
文件“/usr/lib/python3.7/site packages/PyPDF2/pdf.py”,第661行,位于addBookmarkDestination中
parent.addChild(destRef,self)
addChild中的文件“/usr/lib/python3.7/site packages/PyPDF2/generic.py”,第666行
childObj=child.getObject()
getObject中的文件“/usr/lib/python3.7/site packages/PyPDF2/generic.py”,第178行
返回self.pdf.getObject(self.getObject())
AttributeError:“list”对象没有属性“getObject”
那么,什么是正确的方法来完成这项工作呢