docx.opc.exceptions.PackageNotFoundError: Package not found at '文件名.docx' 問題解決


編譯源程序時,提示:docx.opc.exceptions.PackageNotFoundError: Package not found at '文件名.docx' 。

源文件明明存在啊,難道是用的相對路徑不能讀取,於是換了絕對路徑,結果還是提示此錯誤。

tpl = DocxTemplate("123.docx")

到底是什么原因呢?

百度了一下,找到如下提示:【原文:https://www.javaear.com/question/47199300.html】

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.

根據提示,插入上述代碼后再次運行,查看顯示的路徑,便知道問題出在哪里了

C:\Program Files\Notepad++\456.docx

原來程序編譯運行的默認地址是notpad++的安裝目錄,所以導致找不到文件了。

於是,在CMD中重新進入源文件目錄,編譯並運行,結果順利通過編譯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM