I want to open a .docx file with function Document()
. But it always returns:
docx.opc.exceptions.PackageNotFoundError: Package not found at '/home/chaomaer/PycharmProjects/demo/lab1/book1.docx'
However, when I change the function to open()
, it works well.
I want to know "why?"
from docx import Document
import os
document = open('book1.docx')
# document = Document('book1.docx')
print document.read()
I know a simple solution I work in a ubantu Os when the docx raise the error, try copy the file that can not be opened in a new file within MS word. It works for me if someone knows the deep reason. I woud to be appreciate to you.
This happened to me. Turned out that the word document I tried to open with docx was corrupted.
First install pip install python-docx
And then change the code following:
from docx import Document
import os
#document = open('book1.docx')
document = Document('book1.docx')
for p in document.paragraphs:
print p.text
It works for me. Hope this will help.
EDIT:
You should create docx file with MS or other suitable editor. My advice - use google docs if you have account. Creating file with touch
command in the terminal doesn't help.
I've noticed that if I create the Word document by right-clicking in Windows Explorer and selecting New > Word Document, I will forever get the PackageNotFoundError on that document. If, instead, I open Microsoft Word and create the document through there, it works just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With