11

Can't open doxc file by path (Package not found at...)

View of directory:

enter image description here


Code:

from docx import Document

# some path of file:
path = 'TestDir/dir2/doc22.docx'

# open docx file:
doc = Document(path)

Have this:

Traceback (most recent call last):
  File "T23_7.py", line 73, in <module>
    searchRegex(dirName, regex)
  File "T23_7.py", line 57, in searchRegex
    current_doc = Document(docx_file)
  File "/home/ch_dmitriy/Documents/Projects/Tutorials/mech-mat-homework/env/lib/python3.5/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/home/ch_dmitriy/Documents/Projects/Tutorials/mech-mat-homework/env/lib/python3.5/site-packages/docx/opc/package.py", line 116, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/home/ch_dmitriy/Documents/Projects/Tutorials/mech-mat-homework/env/lib/python3.5/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/home/ch_dmitriy/Documents/Projects/Tutorials/mech-mat-homework/env/lib/python3.5/site-packages/docx/opc/phys_pkg.py", line 31, in __new__
    "Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at 'TestDir/dir2/doc22.docx'

Help, please.

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Django Girl
  • 121
  • 1
  • 1
  • 4

10 Answers10

4

This error simply means there is no .docx file at the location you specified.

Since you specified a relative path, the actual path used is determined by adding 'TestDir/dir2/doc22.docx' to the current working directory Python is using at run time.

You can discover the path being used with this short code snippet:

import os
print(os.path.abspath('TestDir/dir2/doc22.docx')

I expect you'll find that it prints out a path that does not exist, and that you'll need to modify the path string you give it to point to the right place.

Worst case, you can specify an absolute path, like /home/ch_dmitriy/Documents/Projects/Tutorials/TestDir/dir2/doc22.docx.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • 1
    I have the same issue @scanny both are the same in my case its showing the same error – Ssravankumar Aditya Oct 08 '18 at 11:31
  • @SsravankumarAditya that would likely indicate that the file is not a .docx file. Try unzipping the .docx file. If that doesn't work it means it's probably the old .doc format. If you can unzip it (verifying it is a zip archive) try loading it (the pre-unzipped version) with Word. – scanny Oct 08 '18 at 18:32
4

I had the same issue with the correct path. What worked for me is to create the .docx file with an empty Document().

document = docx.Document()
document.save('your_doc_name.docx')

So you can do something like :

try:
  document = docx.Document('your_doc_name.docx')
except:
  document = docx.Document()
  document.save('your_doc_name.docx')
  print("Previous file was corrupted or didn't exist - new file was created.")
RedQueen
  • 41
  • 2
1

Another tricky reason in my case for this issue is: DOCX is actually NOT a DOCX file. You can use word processor (such as MS Word) to open file and see the actually file type at the end of the table. To solve this, you can save as the file with DOCX tail so docx can process on it.

Steven Lee
  • 415
  • 4
  • 14
1

I encountered the same problem. Simply closing the document - which was open during the evaluation of the code - did the job for me.

1

docx.opc.exceptions.PackageNotFoundError: package not found

This error is caused by nothing in your docx file, you can just type a few space characters in the file to fix it.

dedy yuan
  • 11
  • 1
0

Try opening the document. In my case, the document was corrupted.

P.Gupta
  • 485
  • 4
  • 18
0

I encountered the same error in python, as I used Os.walk command to search files in Directories (here docx files after importing docx module):

docx.opc.exceptions.PackageNotFoundError: Package not found at

but I did use the complete path, as above Scanny mentioned: It means that the file do not exists, also I print all the file paths before opening them and found out that a few files apparently are temporary created files of word files in windows, when we edit them, they begin all with '~S' and they are not actually exist and this should be the reason of the error. Also I corrected the problem so:

if strfile.endswith('.docx') and not strfile.startswith('~$'):
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
Shahrad S
  • 1
  • 2
0

I got similar problem but worked with virtual environment. My script could not find the source file without full path definition. I found that virtual environment settings pointed to ./venv subdirectory as working directory. So I simply fix the path in PyCharm run settings and it runs. The second solution is simply move source file to ./venv subdirectory .

Junk Qev
  • 1
  • 1
0

Another reason for this problem is that there are multiple hidden files that are not .docx format which is why this error is coming. These files could be starting with ("~$") if these are open files or (".DS") files if you are using macOS.

My error was resolved when I added the following exceptions to my code:

for file in os.listdir(folder_path):
    if file.endswith(".docx") and not file.startswith('~$') and not file.startswith('.DS_'):
        <enter rest of the code here
Michael M.
  • 10,486
  • 9
  • 18
  • 34
-1

i encounter this problem before and the solution can be by adding manually something in the file and delete it .

yokaso75
  • 31
  • 1
  • 5