Python-docx module error on Windows

Question:

I'm using the following code:

from docx import Document
d = Document(open("arquivo.docx"))

Esse código funciona no Ubuntu mas não funciona no windows.

No windows tenho o retorno no terminal:

Traceback (most recent call last):
File “Default2.py”, line 94, in dialogo
d = Document(f)

File “C:\Python27\lib\site-packages\docx\api.py”, line 35, in init
document_part, package = self._open(docx)

File “C:\Python27\lib\site-packages\docx\api.py”, line 193, in _open
package = Package.open(docx)

File “C:\Python27\lib\site-packages\docx\opc\package.py”, line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)

File “C:\Python27\lib\site-packages\docx\opc\pkgreader.py”, line 32, in from_f
ile

phys_reader = PhysPkgReader(pkg_file)
File “C:\Python27\lib\site-packages\docx\opc\phys_pkg.py”, line 101, in __init
__
self._zipf = ZipFile(pkg_file, ‘r’)

File “C:\Python27\lib\zipfile.py”, line 793, in init
self._RealGetContents()

File “C:\Python27\lib\zipfile.py”, line 860, in _RealGetContents
raise BadZipfile(“Truncated central directory”)
zipfile.BadZipfile: Truncated central directory

O código completo está nesse link

Answer:

Para a solução do erro gerado usei o seguinte código na linha 93:

f = open(arquivo,'rb')

"rb" Reads Binary. To learn more, visit the library link

Scroll to Top