Python docx Document()函数未按预期工作

cbeh67ev  于 2023-04-08  发布在  Python
关注(0)|答案(1)|浏览(152)

我正在尝试创建一个名为on_qvr_click的函数,该函数以Excel文件作为输入,并读取名为qvr_files的列。对于该列中的每个单元格,它都会在qvr_templates目录中查找相应的.docx文件。然后,它将这些模板文件的内容组合成一个word文档,并将其保存在qvr_bundle目录中,名称为combined_template.docx。
下面是我的代码:

def on_qvr_click():
    file_path = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx;*.xlsm")])
    if file_path:
        print(f"Selected file: {file_path}")
        bundle_path = os.path.join("qvr_bundle")
        wb = openpyxl.load_workbook(file_path)
        ws = wb.active
        qvr_files_col = None
        for col in ws.iter_cols():
            if col[0].value == "qvr_files":
                qvr_files_col = col
                break
        if qvr_files_col:
            combined_document = Document()
            for cell in qvr_files_col[1:]:
                if cell.value:
                    template_path = os.path.join("qvr_templates", f"{cell.value}.docx")
                    if os.path.exists(template_path):
                        template_document = Document(template_path)
                        for element in template_document.element.body:
                            combined_document.element.body.append(element)
            combined_document.save(os.path.join(bundle_path, "combined_template.docx"))
        messagebox.showinfo("Success", "QVR bundle created successfully!")
        os.startfile(bundle_path)

下面是我的目录结构:

-> envisage
--> qvr_bundle
--> qvr_templates
---> 010AHU.docx
---> 012FCU.docx
--> static_files
--> envisage2.exe

但是当运行程序时,Document()函数抛出一个错误,说它找不到模板.docx文件:

combined_document.save(os.path.join(bundle_path, "combined_template.docx"))  File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\document.py", line 135, in save
    self._part.save(path_or_stream)
  File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\parts\document.py", line 111, in save    self.package.save(path_or_stream)
  File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\opc\package.py", line 172, in save
    PackageWriter.write(pkg_file, self.rels, self.parts)
  File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\opc\pkgwriter.py", line 32, in write
    phys_writer = PhysPkgWriter(pkg_file)
  File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\opc\phys_pkg.py", line 141, in __init__
    self._zipf = ZipFile(pkg_file, 'w', compression=ZIP_DEFLATED)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\zipfile.py", line 1249, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'qvr_bundle\\combined_template.docx'

我不能弄清楚为什么它抛出这个错误

atmip9wb

atmip9wb1#

我设法解决了它,它是如此愚蠢,在这里找到了答案:
https://stackoverflow.com/a/73289012/15163302
我试图合并的两个文档都是空的,这就是为什么它会抛出错误!
我一把东西放进文件里,就奏效了。

相关问题