简体   繁体   English

PyPDF2在Windows上使用Python加密PDF:AttributeError:'tuple'对象没有属性'write'

[英]PyPDF2 Encrypting PDF with Python on Windows: AttributeError: 'tuple' object has no attribute 'write'

Edit to include full Code: Program basics -- Take CSV file of ID/EMAIL/PASSWORD, and create dictionary1. 编辑以包含完整的代码:程序基础知识-提取ID / EMAIL / PASSWORD的CSV文件,并创建dictionary1。 Create a dictionary2 of ID/File in a path. 在路径中创建ID /文件的dictionary2。

Use Dictionary2 to lookup password in Dict1, apply encryption, and finally email using email in Dict1. 使用Dictionary2在Dict1中查找密码,应用加密,最后使用Dict1中的电子邮件发送电子邮件。 I'm stuck at the encryption part at this point. 我现在停留在加密部分。

I've read some questions and google articles about needing to open the file, but I'm not sure how to open the "output" item... 我已经阅读了一些有关需要打开文件的问题和有关Google的文章,但不确定如何打开“输出”项...

Error: 错误:

Traceback (most recent call last):
  File "CommissionSecurity.py", line 54, in <module>
    output.write(outputStream)
  File "build\bdist.win-amd64\egg\PyPDF2\pdf.py", line 472, in write
AttributeError: 'tuple' object has no attribute 'write'

Code: 码:

import os 
import re
import csv
import PyPDF2


# Create a dictionary with the csv values
EmailDict = dict()
with open('commissionrepemaillist.csv', 'r') as infile:
    reader = csv.reader(infile)
    for row in reader :
        REP = row[0]
        EMAIL = row[1]
        PASSWORD = row[2]
        EmailDict[REP] = EMAIL, PASSWORD

# create dictionary of IDs and Pdf Files
FileDict = dict()
path = "C:\\Apps\\CorVu\\DATA\\Reports\\AlliD\\Monthly Commission  Reports\\Output\\pdcom1"  
for FILE in os.listdir(path):
    split = re.split("[_.]", FILE)
    ID = split[1]
    FileDict[ID] = FILE

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 = open(os.path.join(path + FileDict[ID]), 'rb')
        output = PyPDF2.PdfFileWriter()
        input = PyPDF2.PdfFileReader(file)

        print ID, EmailDict[ID][1]  # returning the correct value for encryption
        output.encrypt(EmailDict[ID][1])
        outputStream = (file, "wb")
        output.write(outputStream)

        output.close()
        input.close()

    else : continue
outputStream = (file, "wb")

This just creates a tuple with a file and a string. 这只会创建一个带有文件和字符串的元组。 The PDF writer write method doesn't know what to do with an arbitrary tuple, it expects a file-like object. PDF writer编写方法不知道如何处理任意元组,它需要一个类似文件的对象。 You probably wanted to instead open that file for writing instead: 您可能想改为打开该文件以进行写入:

outputStream = open(file.name, "wb")

Now taking the name from an existing file object and reusing it is probably not the best way to do this. 现在,从现有文件对象中获取名称并重用它可能不是执行此操作的最佳方法。 Generate the file path once then reuse that for opening the original file then use again for writing out to the PDF file you have processed. 生成一次文件路径,然后重新使用该路径来打开原始文件,然后再次用于写出已处理的PDF文件。

Also your code would be improved by using the file context managers . 此外,通过使用文件上下文管理器可以改善您的代码。

with open(file, "wb") as outputStream:
    output.write(outputStream)

This way your files will be closed at the end of the statement and your file resources will be handled properly. 这样,您的文件将在语句末尾关闭,并且文件资源将得到正确处理。 It also reduces the code maintenance cost because you don't need to pair open and close statements for the files as that is now done for you. 这也减少了代码维护成本,因为您不需要像现在那样为文件配对openclose语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python PyPDF2 'PdfFileReader' object 没有属性 'scaleTo' 错误 - Python PyPDF2 'PdfFileReader' object has no attribute 'scaleTo' error AttributeError: 'tuple' object 没有属性 'write',实例分段 python - AttributeError: 'tuple' object has no attribute 'write' , instance segmentation python Python 错误:AttributeError: 'tuple' object 没有属性 'write' - Python error: AttributeError: 'tuple' object has no attribute 'write' AttributeError: 'tuple' object has no attribute 'write' 错误 - AttributeError: 'tuple' object has no attribute 'write' Error AttributeError:&#39;tuple&#39;对象没有属性&#39;write&#39; - AttributeError: 'tuple' object has no attribute 'write' 使用PyPDF2通过python加密许多PDF - Encrypting many PDFs by python using PyPDF2 Python AttributeError:“元组”对象没有属性“ print” - Python AttributeError: 'tuple' object has no attribute 'print' Python:AttributeError:&#39;tuple&#39;对象没有属性&#39;setdefault&#39; - Python: AttributeError: 'tuple' object has no attribute 'setdefault' AttributeError: &#39;tuple&#39; 对象没有属性 &#39;encode&#39; python - AttributeError: 'tuple' object has no attribute 'encode' python Python:AttributeError:&#39;tuple&#39;对象没有属性&#39;read&#39; - Python: AttributeError: 'tuple' object has no attribute 'read'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM