Python-docx module error in Windows

Asked

Viewed 232 times

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

The complete code is at that link

2 answers

1


To solve the error I used the following code on line 93:

f = open(arquivo,'rb')

"Rb" Reads Binary. To learn more please visit link library

0

Try to put it like that on line 93:

f = open(arquivo,'w')

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

  • @Math his answer helped me a lot. However, it was not the right one, but it gives me paths where I should continue researching. Anyway I changed to the form that served to min. Thank you.

  • @Muriloferro you can post your own answer explaining what you did to solve, if you think it’s worth explaining.

  • Excellent response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.