我正在尝试使用zipfile在Python上读取受密码保护的Word文档。 以下代码适用于非密码保护的文档,但在与受密码保护的文件一起使用时会出错。
try:
from xml.etree.cElementTree import XML
except ImportError:
from xml.etree.ElementTree import XML
import zipfile
psw = "1234"
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
def get_docx_text(path):
document = zipfile.ZipFile(path, "r")
document.setpassword(psw)
document.extractall()
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text
for node in paragraph.getiterator(TEXT)
if node.text]
if texts:
paragraphs.append(''.join(texts))
return '\n\n'.join(paragraphs)
使用受密码保护的文件运行get_docx_text()时,收到以下错误:
追踪(最近一次呼叫最后一次):
File "<ipython-input-15-d2783899bfe5>", line 1, in <module>
runfile('/Users/username/Workspace/Python/docx2txt.py', wdir='/Users/username/Workspace/Python')
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 680, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/username/Workspace/Python/docx2txt.py", line 41, in <module>
x = get_docx_text("/Users/username/Desktop/file.docx")
File "/Users/username/Workspace/Python/docx2txt.py", line 23, in get_docx_text
document = zipfile.ZipFile(path, "r")
File "zipfile.pyc", line 770, in __init__
File "zipfile.pyc", line 811, in _RealGetContents
BadZipfile: File is not a zip file
有没有人有任何建议让这段代码有效?
感谢您分享您的知识和专长。
答案 0 :(得分:1)
我认为这不是加密问题,原因有两个:
创建ZipFile
对象时未尝试解密。像ZipFile.extractall
,extract
和open
以及read
这样的方法会使用包含密码的可选pwd
参数,但对象构造函数/初始化程序不会。 / p>
您的堆栈跟踪表示在您调用BadZipFile
之前创建ZipFile
对象时会引发setpassword
:
document = zipfile.ZipFile(path, "r")
我会仔细查看您正在测试的两个文件之间的其他差异:所有权,权限,安全上下文(如果您的操作系统上有此文件),...甚至文件名差异都可能导致框架“看不到” “你正在处理的文件。
另外---显而易见的一个---尝试使用与zip兼容的命令打开加密的zip文件。看看它是否 是一个zip文件。
我通过在Python 3.1中打开加密的zip文件进行测试,同时“忘记”提供密码。我可以创建ZipFile
对象(下面的变量zfile
)而没有任何错误,但得到RuntimeError
--- 而不是 BadZipFile
例外---当我试图在没有提供密码的情况下读取文件时:
Traceback (most recent call last):
File "./zf.py", line 35, in <module>
main()
File "./zf.py", line 29, in main
print_checksums(zipfile_name)
File "./zf.py", line 22, in print_checksums
for checksum in checksum_contents(zipfile_name):
File "./zf.py", line 13, in checksum_contents
inner_file = zfile.open(inner_filename, "r")
File "/usr/lib64/python3.1/zipfile.py", line 903, in open
"password required for extraction" % name)
RuntimeError: File apache.log is encrypted, password required for extraction
我还能够提出BadZipfile
异常,一次尝试打开一个空文件,一次尝试打开一些我重命名为“.zip”扩展名的随机日志文件。这两个测试文件产生相同的堆栈跟踪,直到行号。
Traceback (most recent call last):
File "./zf.py", line 35, in <module>
main()
File "./zf.py", line 29, in main
print_checksums(zipfile_name)
File "./zf.py", line 22, in print_checksums
for checksum in checksum_contents(zipfile_name):
File "./zf.py", line 10, in checksum_contents
zfile = zipfile.ZipFile(zipfile_name, "r")
File "/usr/lib64/python3.1/zipfile.py", line 706, in __init__
self._GetContents()
File "/usr/lib64/python3.1/zipfile.py", line 726, in _GetContents
self._RealGetContents()
File "/usr/lib64/python3.1/zipfile.py", line 738, in _RealGetContents
raise BadZipfile("File is not a zip file")
zipfile.BadZipfile: File is not a zip file
虽然这个堆栈跟踪与完全与你的相同 - 我的呼叫_GetContents
和3.2之前的“小f”拼写{{1但是它们足够接近我认为这是你正在处理的那种问题。