Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python docx.opc.exceptions.PackageNotFoundError: Package not found when opening Document

Tags:

python

docx

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()
like image 652
chaomaer Avatar asked Apr 24 '17 02:04

chaomaer


4 Answers

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.

like image 199
chaomaer Avatar answered Oct 29 '22 13:10

chaomaer


This happened to me. Turned out that the word document I tried to open with docx was corrupted.

like image 32
Charl Swart Avatar answered Oct 29 '22 14:10

Charl Swart


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.

like image 1
R.A.Munna Avatar answered Oct 29 '22 15:10

R.A.Munna


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.

like image 1
Tom Avatar answered Oct 29 '22 14:10

Tom