关于python编程中路径错误导致文档无法写入的解决方案
在编写python程序时,有时会遇到因路径错误而无法将文档写入指定路径的问题。今天,我们将讨论如何解决这种问题,特别是在将多张图片自动排版并写入word文档的过程中。
首先,展示一下实现这一功能的代码:
import os from pil import image from docx import document from docx.shared import inches from docx.enum.text import wd_paragraph_alignment from docx.oxml import oxmlelement def create_word_document(image_folder, output_path): # 获取图片文件列表 image_files = [f for f in os.listdir(image_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))] if not image_files: print("未找到任何图片文件。") return # 创建word文档 doc = document() doc.add_heading('照片', level=1).alignment = wd_paragraph_alignment.center # 每页排版两张照片 photos_per_page = 2 for i, image_file in enumerate(image_files, start=1): if i % photos_per_page == 1: # 添加新的一页 section = doc.sections[-1] footer = section.footer footer.paragraphs[0].clear() # 清除页脚原有内容 footer.paragraphs[0].add_run(f"第 {i // photos_per_page + 1} 页").alignment = wd_paragraph_alignment.center doc.add_page_break() # 添加图片和备注 img_path = os.path.join(image_folder, image_file) img = image.open(img_path) doc.add_picture(img_path, width=inches(3.0)) doc.add_paragraph(f"序号:{i}") doc.add_paragraph("备注:") # 保存word文档 doc.save(output_path) print(f"word文档已保存至: {output_path}") if __name__ == "__main__": # 指定图片文件夹和输出word文档路径 image_folder_path = "d:anzhuang/python/ima" output_word_path = "d:anzhuang/python/output/" create_word_document(image_folder_path, output_word_path)
运行上述代码时,如果遇到类似下面的错误:
d:anzhuangpythonpython.exe c:/users/admin/pycharmprojects/pythonproject/lianxi3.py traceback (most recent call last): file "c:usersadminpycharmprojectspythonprojectlianxi3.py", line 51, in <module> create_word_document(image_folder_path, output_word_path) file "c:usersadminpycharmprojectspythonprojectlianxi3.py", line 40, in create_word_document doc.save(output_path) file "d:anzhuangpythonlibsite-packagesdocxdocument.py", line 151, in save self._part.save(path_or_stream) file "d:anzhuangpythonlibsite-packagesdocxpartsdocument.py", line 106, in save self.package.save(path_or_stream) file "d:anzhuangpythonlibsite-packagesdocxopcpackage.py", line 151, in save packagewriter.write(pkg_file, self.rels, self.parts) file "d:anzhuangpythonlibsite-packagesdocxopcpkgwriter.py", line 27, in write phys_writer = physpkgwriter(pkg_file) file "d:anzhuangpythonlibsite-packagesdocxopcphys_pkg.py", line 109, in __init__ self._zipf = zipfile(pkg_file, "w", compression=zip_deflated) file "d:anzhuangpythonlibzipfile.py", line 1239, in __init__ self.fp = io.open(file, filemode) permissionerror: [errno 13] permission denied: 'd:anzhuang/python/output/'
我们尝试通过提升管理员权限来解决无法写入给定路径的问题,但仍然未能解决。这里我们来看看问题出在哪里。
经过仔细检查,发现问题在于输出路径的写法。原代码中的路径是:
立即学习“Python免费学习笔记(深入)”;
output_word_path = "d:anzhuang/python/output/"
正确的方式应该是:
output_word_path = "d:/anzhuang/python/output/"
在windows系统中,当路径是相对盘符(例如,d:)时,必须在盘符后紧跟一个/,否则系统会将路径解释为当前工作目录下的相对路径,而不是绝对路径。
因此,修改路径后,代码应如下所示:
if __name__ == "__main__": # 指定图片文件夹和输出Word文档路径 image_folder_path = "d:/ANZHUANG/PYTHON/IMA" output_word_path = "d:/ANZHUANG/PYTHON/output/" create_word_document(image_folder_path, output_word_path)
通过这个简单的修改,应该能够解决路径错误导致的文档无法写入问题,并成功实现将多张图片拖入固定文件夹后自动用word进行排版的功能。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END