[英]PyPDF2: Why does PdfFileWriter forget changes I made to a document?
我正在嘗試修改PDF文件中的文本。 文本可以在Tj
或BDC
類型的對象中。 我找到了正確的對象,如果在更改它們后直接閱讀它們,它們將顯示更新的值。
但是,如果我將整個頁面傳遞給PdfFileWriter,更改將丟失。 我可能正在更新副本,而不是真實對象。 我檢查了id()
,結果有所不同。 有人知道如何解決此問題嗎?
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import ContentStream
from PyPDF2.generic import TextStringObject, NameObject
from PyPDF2.utils import b_
source = PdfFileReader(open('some.pdf', "rb"))
output = PdfFileWriter()
for page_idx in range(0, 1):
# Get the current page and it's contents
page = source.getPage(page_idx)
content_object = page["/Contents"].getObject()
content = ContentStream(content_object, source)
for operands, operator in content.operations:
if operator == b_("BDC"):
operands[1][NameObject('/Contents')] = TextStringObject('xyz')
if operator == b_("Tj"):
operands[0] = TextStringObject('xyz')
output.addPage(page)
# Write the stream
outputStream = open("output.pdf", "wb")
output.write(outputStream)
outputStream.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.