我已使用以下代码成功将注释从一个 pdf 复制到另一个 pdf:
def copy_and_update_annotations(src_pdf_file,dest_pdf_file):
#copies annotations from scr_file to a destination file
reader = PdfReader(src_pdf_file)
dest_reader=PdfReader(dest_pdf_file)
writer=PdfWriter()
first_page=dest_reader.pages[0]
writer.add_page(first_page)
for page in reader.pages:
if "/Annots" in page:
for annot in page["/Annots"]:
obj = annot.get_object()
writer.add_annotation(page_number=0, annotation=obj)
writer.write(dest_pdf_file)
在下一步中,我想更改注释的一些属性,例如“作者”字段(obj['/T'])。但这一次,我在最后一行得到 'AttributeError: 'str' object has no attribute 'write_to_stream' :
def copy_and_update_annotations(src_pdf_file,dest_pdf_file):
#copies annotations from scr_file to a destination file
reader = PdfReader(src_pdf_file)
dest_reader=PdfReader(dest_pdf_file)
writer=PdfWriter()
first_page=dest_reader.pages[0]
writer.add_page(first_page)
for page in reader.pages:
if "/Annots" in page:
for annot in page["/Annots"]:
obj = annot.get_object()
if '/T' in obj: # Update Author
obj.update({'/T': 'NEW Author'}) #Update Author
writer.add_annotation(page_number=0, annotation=obj)
writer.write(dest_pdf_file) # -->Here, I get: 'AttributeError: 'str' object has no attribute 'write_to_stream'
我尝试了不同的方法,例如 dest_pdf_file.encode() 或将 dest_pdf 转换为 StringIO 或 BytesIO。到目前为止没有任何效果。我正在使用 pypdf-3.12.0 和 python 3.9 64 位。
我做错了什么?
*** 更新:添加时
with open(dest_pdf_file, "wb") as output_stream:
writer.write(output_stream)
我仍然遇到同样的错误。也许包本身有错误? 请在此处查看完整的错误消息:
File "C:\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py", line 1287, in write
self.write_stream(stream)
File "C:\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py", line 1260, in write_stream
object_positions = self._write_pdf_structure(stream)
File "C:AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py", line 1306, in _write_pdf_structure
obj.write_to_stream(stream)
File "C:\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\generic\_data_structures.py", line 369, in write_to_stream
value.write_to_stream(stream)
AttributeError: 'str' object has no attribute 'write_to_stream'
我认为这是因为你试图用“string”类型编写“/T”,而实际上在 pypdf 中它是“TextStringObject”类型。您应该将字符串转换为这种类型,例如这样:
obj.update({'/T': pypdf.generic._base.TextStringObject('NEW Author')}) #Update Author