PyPDF2.PdfFileWriter addAttachment不工作

2024-10-01 15:48:10 发布

您现在位置:Python中文网/ 问答频道 /正文

基于https://programtalk.com/python-examples/PyPDF2.PdfFileWriter/,示例2,我尝试将附件添加到PDF文件中。在

下面是我要运行的代码:

import os
import PyPDF2
from django.conf import settings

...

doc = os.path.join(settings.BASE_DIR, "../media/SC/myPDF.pdf")

unmeta = PyPDF2.PdfFileReader(doc, "rb")

meta = PyPDF2.PdfFileWriter()
meta.appendPagesFromReader(unmeta)

meta.addAttachment("The filename to display", "The data in the file")

with open(doc, 'wb') as fp:
    meta.write(fp)

当我运行这段代码时,我得到:“TypeError:a bytes like object is required,not'str'”。在

如果我更换

^{pr2}$

签署人:

with open(doc, 'wb') as fp:
    meta.write(b'fp')

我得到这个错误:“'bytes'对象没有'write'属性。”。在

如果我尝试:

with open(doc, 'w') as fp:
    meta.write(fp)

我得到这个错误:“write()参数必须是str,而不是字节”

有人能帮我吗?在


Tags: the代码importdocsettingsosaswith

热门问题