11

I have a doc.docx file at '/var/code/oa'. I need to read it use python-docx. I write this:

from docx import Document
document = Document('/var/code/oa/doc.docx')

then, have error.. PackageNotFoundError: Package not found at '/var/code/oa/doc.docx'

why?

Thanks @soon. Uh, It's stupid. the reason is the file, it's must be docx file. I just change the file name from doc to docx, it's not a really docx file.

ooooh326
  • 133
  • 1
  • 1
  • 13

3 Answers3

9

If there is nothing in your doc.docx, it will raise PackageNotFoundError. Try to put something in it and do it again. Meanwhile, an invalid docx file will cause this error too. Tell me if it works.

Stephen Lin
  • 4,852
  • 1
  • 13
  • 26
  • It has more content, has one table. – ooooh326 Sep 03 '14 at 03:26
  • Thank u very much and it's fixed. The problem is the file, it is not a really 'docx' file. --! – ooooh326 Sep 03 '14 at 03:50
  • @dsphoebe Yeah. Since you don't answer soon, I thought it didn't work for you. I've updated my answer. You can accept it so that anyone else could find this answer easily. Thanks! – Stephen Lin Sep 03 '14 at 05:32
  • Check the path as well, the error (same as OP) is misleading if the path is wrong I lost too many hours with this... – miguelmpn Dec 07 '22 at 09:50
2

In addition to the above answers, 'package not found' error also occurs:

If the docx file is already opened in the windows, while running the python program. So closing all the docx files, restarting the kernel and running the python program again might also solve the error.

The error is also caused If the program is running in any other softwares except Windows (Linux, Ubuntu, ... ).

Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
  • Sometimes when the word document is open in word there is no error, and sometimes I get a page not found error. I am not sure why, just wanted to let everyone know. – Rivet174 Mar 13 '23 at 17:35
0

The other reason for this error is absence of file stream or file object not created. So following code should be one of the alternative fix.

file_ref = open("/var/code/oa/doc.docx","rb")
doc = docx.Document(file_ref)
Samiie
  • 124
  • 4