0
基於https://programtalk.com/python-examples/PyPDF2.PdfFileWriter/,示例2,我嘗試將附件添加到PDF文件中。PyPDF2.PdfFileWriter addAttachment不起作用
這裏是我的代碼,我試圖運行:
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)
當我運行這段代碼,我得到:「類型錯誤:一類字節對象是必需的,而不是‘海峽’」。
如果我更換
with open(doc, 'wb') as fp:
meta.write(fp)
由:
with open(doc, 'wb') as fp:
meta.write(b'fp')
我得到這個錯誤: 「 '字節' 對象有沒有屬性 '寫'」。
如果我嘗試:
with open(doc, 'w') as fp:
meta.write(fp)
我得到這個錯誤: 「寫()參數必須海峽,而不是字節」
誰能幫助我?