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

8

13

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()
Anglosaxon answered 24/4, 2017 at 2:20 Comment(2)
A .docx format is a collection of heavily interdependent XML files. I would recommend looking into a library such as python-docx.readthedocs.io/en/latest.Palmirapalmistry
It seems strange. Some of the .docx file can be opend with the function Document(),but some of the .docx file can not . It still raise the error like this:docx.opc.exceptions.PackageNotFoundError: Package not found at 'book1.docx'Anglosaxon
A
3

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.

Anglosaxon answered 24/4, 2017 at 4:5 Comment(4)
If the path directory of the .docx file are wrongly given than also raise the same error.Anthotaxy
Indeed. But I am sure the path is rigthAnglosaxon
I would like to know you that all I tried from my windows PC and gives the same error if the path is wrong. Thanks.Anthotaxy
@Anglosaxon Seeing the same problem you described here where some .docx files open fine and others get this error. Can you clarify your Ubuntu solution above? It sounds like you manually open the file in MS Word... do you have an automated solution in Ubuntu?Protean
A
1

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.

Anthotaxy answered 24/4, 2017 at 2:56 Comment(6)
It seems strange. Some of the .docx file can be opend with the function Document(),but some of the .docx file can not . It still raise the error like this:docx.opc.exceptions.PackageNotFoundError: Package not found at 'book1.docx'Anglosaxon
ok.It seems your package has not installed successfully. will you please tell me which python version you are using?? if you use python3.x than install the package pip3 install python-docx.Anthotaxy
I use ther version python 2.7Anglosaxon
ok it's sounds good. I have also done it on my pc with version python2.7.x. Have you execute from command line pip install python-docx? if you execute this command successfully than please check in C:\Python27\Lib\site-packages` have a folder named docx`. If all is okey than the above code will run successfully. Thank you.Anthotaxy
all the conditions you have mentioned I have checked is correct. But the answer is it still not works well. The file is still can not be found.It is there something wrong with my file .but I can open the file with MS word .I am very confused now. Finally, I am very thankful to you.Anglosaxon
Put the book1.docx and somthing.py file in the same directory. or do change document = Document('pathlocation\\book1.docx'). Thank you.Anthotaxy
E
1

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.

Emission answered 11/6, 2018 at 16:39 Comment(0)
W
1

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

Weariful answered 21/10, 2019 at 9:51 Comment(0)
K
0

I found that if I had the word document open at the same time - even if I wasn't doing anything to it, it still threw this error. Closing the word document solved it for me.

Kate answered 26/7, 2020 at 16:54 Comment(0)
P
0

Writing Word Documents

The following instruction where found in the book "automatetheboringstuff".

  • To create your own .docx file, call docx.Document() to return a new, blank Word Document object.
  • The add_paragraph() document method adds a new paragraph of text to the document and returns a reference to the Paragraph object that was added.
  • When you’re done adding text, pass a filename string to the save() document method to save the Document object to a file.
  • This will create a file named helloworld.docx in the current working directory.

Enter the following into the interactive shell:

>>> import docx
>>> doc = docx.Document()
>>> doc.add_paragraph('Hello world!')
<docx.text.Paragraph object at 0x0000000003B56F60>
>>> doc.save('helloworld.docx')

Seems that many of us where creating document with name, as we called the open object function, where as the docx library creates an empty file without name when open object function is called. location and Name needs to be added while saving the document.

Paz answered 24/1, 2021 at 20:50 Comment(0)
D
0

At first, I was facing the same issue but not on all Docx files only a few files. then I printed the local path of each file and found out that whenever I hit the PackageNotFoundError issue. Most of the time these files are temp Docx files and can't be open/read even from Microsoft applications.

Below is my code:

def _RsDocx_(SearchString, Path):
flag = 0
index = 0
doc = docx.Document(Path)
for para in doc.paragraphs:
    index += 1
    if SearchString in para.text:
        flag = 1
        break
if flag != 0:
    print('String', SearchString, 'Found In Line: ', index, 'File Path: ', Path)

I already Imported Docx lib and calling this def from the main

Doldrums answered 2/4, 2021 at 10:28 Comment(0)
Y
0

you can try to open the docx file in microsoft word app, maybe it has something wrong and cannot be opened.

Yarak answered 19/6 at 7:59 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewCynthiacynthie

© 2022 - 2024 — McMap. All rights reserved.