Error in the Python-docx module in Windows

3

I'm using the following code:

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

This code works on Ubuntu but does not work on windows.

In windows I have the return in the terminal:

  

Traceback (most recent call last):     File "Default2.py", line 94, in dialog       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

The full code is on this link

    
asked by anonymous 02.02.2015 / 13:59

2 answers

1

For the solution of the generated error I used the following code in line 93:

f = open(arquivo,'rb')

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

    
09.02.2015 / 00:23
0

Try to put it on line 93:

f = open(arquivo,'w')

or instead of w , put r+ . For w is to create a new file or overwrite it, and r+ is for reading and inserting data.

    
02.02.2015 / 14:56