Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyPDF2:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到PdfielWriter_Python_Python 2.7_Typeerror - Fatal编程技术网

Python PyPDF2:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到PdfielWriter

Python 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,

修改代码,通过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, 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