当前位置: 首页 > 知识库问答 >
问题:

从另一个PDF中替换至少100页的PDF

曾鸿振
2023-03-14

这是一个代码示例,

import PyPDF2
import numpy as np
# creating a pdf file object
pdfFileObj = open('original.pdf' , 'rb')

pdfFileObj_1 = open('tutorial.pdf', 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdfReader_1 = PyPDF2.PdfFileReader(pdfFileObj_1)
# creating a pdf writer object for new pdf
pdfWriter = PyPDF2.PdfFileWriter()
for i in range(100):
    page= pdfReader.getPage(i)
    page_1= pdfReader_1.getPage(i)
    pdfWriter.addPage(page)
    pdfWriter.addPage(page_1)

#print(pdfWriter.getNumPages())
# new pdf file object
newFile = open('replaced_pdf_1.pdf', 'wb')


pdfWriter.write(newFile)

# closing the original pdf file object
pdfFileObj.close()
pdfFileObj_1.close()
# closing the new pdf file object
newFile.close()

我犯的错误,

[pdf.py:1629]Traceback(最近的最后一次调用):文件test.py,第22行,在pdfWriter.write(newFile)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第482行,在写自己。_sweepIndirectReferences(extranalReourceMap,自己。_root)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第571行,在_sweepIndirectReferences自己。_sweepIndirectReferences(extern Map,realdata)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第547行,_sweepIndirectReferences值=自己。_sweepIndirectReferences(extern Map,value)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第571行,_sweepIndirectReferences自己。_sweepIndirectReferences(extern Map,realdata)File"/home/ubuntu/Ritesh/working/lib/python3.5/site-packages/PyPDF2/pdf.py",第547行,_sweepIndirectReferences值=自我。_sweepIndirectReferences(extern Map,value)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第556行,_sweepIndirectReferences值=自我。_sweepIndirectReferences(extern Map, data[i])File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第571行,_sweepIndirectReferences。_sweepIndirectReferences(extern Map, realdata)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第547行,在_sweepIndirectReferences值=自己。_sweepIndirectReferences(extern Map,value)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py",第577行,_sweepIndirectReferencesnewobj=data.pdf.get对象(数据)File"/home/ubuntu/Ritesh/work/lib/python3.5/site-包/PyPDF2/pdf.py”,第1631行,在getObject提升utils中。无法找到对象。PyPDF2.utils.PdfReadError:找不到对象。

我从改变添加PdfFileWriter对象pdfWriter的页数中了解到什么...我需要复制超过100页。

共有1个答案

拓拔辰钊
2023-03-14

我在Windows 10和Red Hat Enterprise Linux 6上使用了此示例代码。在这两种平台上,我都使用了Python2.7(我的工作站上没有Python3.5)。因为您没有提供您的原始版本。pdf和教程。我用了两本pdf格式的电子书:分别686页和1014页。

我无法证实你的观察结果:与

对于范围(100)内的i:

替换为

对于范围(600)内的i:

我收到了1200页的pdf输出。

 类似资料:
  • 如何替换PDF中的图像与另一个图像PDF框。怎么做? 我想用另一个图像更改pdf上的VisualSignature。

  • 我正在将一些PDF编辑工具从iTextSharp更新到iText7。 在iTextSharp中,pdfStamper类中有一个方法:pdfStamper。ReplacePage()。然而,iText7中的pdfStamper类已被删除,我在复制相同的功能时遇到困难。 假设我有文档X,需要用文档Y的第1页替换第4页,但我希望结果保存到一个新的文档Z。 到目前为止,我有3行使用CopyPageTo方法

  • 当我使用带电子令牌的IText对PDF进行签名时,签名的PDF在Acrobat Adobe Reader中显示“至少一个签名无效”。我正在使用有效的电子代币。以下是签署Pdf的代码。

  • 问题内容: 什么功能可以用另一个字符串替换一个字符串? 实例1:将替代哪些用? 例2:将替代哪些用? 问题答案: 该方法是你要寻找的。 例如:

  • 我使用BIRT报告创建了一个PDF,每个数据页的标题中都包含一个表。现在我使用IText将目录添加到PDF报告中。有没有办法使用IText将标题从数据页复制到TOC页?

  • 我成功地使用Apache Batik将SVG文件转换为PDF。 以下代码用于生成 PDF: 现在我想影响生成的PDF的页面大小,所以我得到了A4的页面大小。我怎么能这么做呢? 我已经尝试了一些关键提示,但没有效果。