python docx找不到docx文件

问题描述 投票:1回答:1

我实际上在我的文件夹中有文件:enter image description here

我的代码是:

#-*-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)

当我运行它时,它返回的错误是:

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'

请帮助

python python-docx
1个回答
0
投票

似乎对于其他文件格式(如docx,xl​​sx和pdfs,该文件应位于当前工作目录中。所以你可以做:

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

然后查看是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.