gpt4 book ai didi

python - PDF 创建和在其中写入内容 - PyPDF2

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:21 24 4
gpt4 key购买 nike

我正在递归地读取一个 pdf 中的文本,并在每次运行时对提取的文本进行一些操作,并希望创建一个新的 pdf 来保存每次运行时编辑的文本。我在 PyPDF2 中尝试了以下..

import PyPDF2
output = PdfFileWriter()
pdf="pdfte.pdf"
Obj_pdfFile = open(pdf, 'rb')
pdfReader = PyPDF2.PdfFileReader(Obj_pdfFile,strict = False)
pages=pdfReader.numPages
for page in range(pages):
pageObj = pdfReader.getPage(page)
pdf_text=pageObj.extractText()
upper = pdf_text.upper()
#print(pdf_text)
output.addPage(input.getPage(upper)) . # I thought this will work but no use..

我知道需要输入“页面”,但基本上是在寻找如何在新的 pdf 中保存编辑后的文本...我知道我在这里缺少一些代码,如何在 pdf 中保存等,但这正是我需要帮助的,从来没有工作过有pdf..另外,有没有更好的选择来做到这一点?

最佳答案

PyPDF2 非常适合将 pdf 文件作为文档处理,但不能作为编辑器。我想做你尝试过的同样的事情,但只能通过reportlab实现,就像这里的许多其他答案一样。注意这里

output.addPage(input.getPage(upper)) . # I thought this will work but no use. upper is a string, and getPage() is expecting a page from PyPDF2.PdfFileReader(pdffile).getPage(0) Here is that worked for me on python 2.7:

    temp = StringIO()
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A6 #choose here your size
can = canvas.Canvas(temp, pagesize=A6)
can.drawString(10, 405, "Your string on this position")
can.save()
temp.seek(0)
lector = PyPDF2.PdfFileReader(temp)
output.addPage(lector.getPage(0)) #your pypdf2 writter

现在输出是您的 pdf 并附加了字符串,希望有人发现它有用。

关于python - PDF 创建和在其中写入内容 - PyPDF2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52306584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com