panda001
panda001

Reputation: 109

python docx can not find docx file

I actually have the file in my folder: enter image description here

my code is :

#-*-coding:utf-8-*-
import re 
import time
import datetime
import sys
import os
import csv
import docx

from docx import Document
from docx import *  

CURRENT_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))
docxFilePath = os.path.join(CURRENT_DIR,'111.docx')
doc=Document(docxFilePath)

when I run it , it returns me error is:

Traceback (most recent call last):   File
"C:\Users\Windows\Desktop\test\fp\makereport.py", line 20, in <module>
    doc=Document(docxFilePath)   File "C:\Python27\lib\site-packages\docx\api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part   File "C:\Python27\lib\site-packages\docx\opc\package.py", line 116, in open
    pkg_reader = PackageReader.from_file(pkg_file)   File "C:\Python27\lib\site-packages\docx\opc\pkgreader.py", line 32, in
from_file
    phys_reader = PhysPkgReader(pkg_file)   File "C:\Python27\lib\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
'C:\Users\Windows\Desktop\test\fp\111.docx'

please help

Upvotes: 1

Views: 2716

Answers (2)

Pooja
Pooja

Reputation: 1

I have encountered the same problem and Scanny has answered it correct that file was found but was not a real .docx file.

Don't create it in any other application and rename it to .docx but create a real .docx file. You can use below to create one using code.

doc = docx.Document()
doc.save("/path/to/file/where/it/needs/to/save/.docx")

Upvotes: 0

Sagarbade Shrestha
Sagarbade Shrestha

Reputation: 11

It seems for other file formats as docx, xlsx, and pdfs the file should be in the current working directory. So u can do :

import os
os.chdir('C://Users/Windows/Desktop/test/fp')

Then see if it works.

Upvotes: 1

Related Questions