Python PyPDF2:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到PdfielWriter
修改代码,通过with语句包含上下文管理器。然而,我收到了一个回溯:在Windows上使用Python 2.7Python PyPDF2:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到PdfielWriter,python,python-2.7,typeerror,Python,Python 2.7,Typeerror,修改代码,通过with语句包含上下文管理器。然而,我收到了一个回溯:在Windows上使用Python 2.7 Traceback (most recent call last): File "CommissionSecurity.py", line 52, in <module> with open(output, 'w') as outputStream : TypeError: coercing to Unicode: need string or buffer,
Traceback (most recent call last):
File "CommissionSecurity.py", line 52, in <module>
with open(output, 'w') as outputStream :
TypeError: coercing to Unicode: need string or buffer, PdfFileWriter found
open()函数需要一个字符串。
PyPDF2.PdfFileWriter()
是否返回字符串?您需要检查PyDF2.PdfFileWriter()
返回的是什么?以open(output,'w')作为outputStream
,而不是output
,例如使用文件名“C:myfile.pdf
谢谢!我用文件替换了输出,这就解决了这个问题,转到下一个回溯。您是否应该使用“wb
”作为outputStream
?
for ID in FileDict:
# print REP
# print ID # debug: REP always coming over 764
if ID in EmailDict :
#print FileDict[ID]
path = "C:\\Apps\\CorVu\\DATA\\Reports\\AlliD\\Monthly Commission Reports\\Output\\pdcom1\\"
file = os.path.join(path + FileDict[ID])
output = PyPDF2.PdfFileWriter()
input = PyPDF2.PdfFileReader(file)
outputStream = (file, "wb")
with open(file, 'rb') as input :
with open(output, 'w') as outputStream :
output.encrypt(EmailDict[ID][1])
output.write(outputStream)
else : continue