简体   繁体   English

如何使用 python 替换 PDF 中的文本

[英]How to replace text in a PDF using python

from PyPDF2 import PdfFileReader, PdfFileWriter

replacements = [
    ("old string", "new string")
]

pdf = PdfFileReader(open(r'C:\Users\DELL-PC\Desktop\text augmentation.pdf', "rb"))
writer = PdfFileWriter() 

for page in pdf.pages:
    contents = page.getContents().getData()
    for (a,b) in replacements:
        contents = contents.replace(a.encode('utf-8'), b.encode('utf-8'))
    page.getContents().setData(contents)
    writer.addPage(page)
    
with open("modified.pdf", "wb") as f:
     writer.write(f)

pypdf (and also PyPDF2/PyPDF3/PyPDF4) cannot do that. pypdf (以及 PyPDF2/PyPDF3/PyPDF4)无法做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM