0

I am trying to make a function called on_qvr_click that takes an Excel file as input and reads the column named qvr_files. For each cell in this column, it looks for a corresponding .docx file in the qvr_templates directory. It then combines the contents of these template files into a single word document and saves it in the qvr_bundle directory with the name combined_template.docx.

Here is my code:

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)

And here is my directory structure:

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

But when running the program the Document() function throws an error, saying it cannot find the template .docx file:

    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'

I cant figure out why it is throwing this error

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Did you check that there is `qvr_templates\010AHU.docx` in the current working directory? This looks like problem with relative path. i.e. is `envisage` your current working directory? – buran Apr 05 '23 at 12:52
  • To put it a different way: what directory is `envisage2.py` in? You removed all the useful path information from the traceback. Please don't do that. You can remove your username if you want, but the [*full text* of any errors or tracebacks](https://meta.stackoverflow.com/q/359146) are needed to successfully troubleshoot. – MattDMo Apr 05 '23 at 13:01
  • @buran yes envisage is the current working directory. That is where the script envisage2.py is located. Apologies in my directory structure I named it "envisage" instead of "envisage2" I used pyinstaller to compile it into an executable. – Ruben Adriaanse Apr 05 '23 at 14:56
  • @MattDMo envisage2.py is in the envisage directory. Apologies in my directory structure I named it "envisage" instead of "envisage2" I used pyinstaller to compile it into an executable. I will update my question with the full error log. – Ruben Adriaanse Apr 05 '23 at 14:57
  • 1
    Oh, you're running this inside a PyInstaller bundle. That's important information. – MattDMo Apr 05 '23 at 15:09
  • _That is where the script envisage2.py is located_ - that is not necessary the CWD. CWD is the active directory from which you run the program. As already said - running an exe bundled with pyinstaller is important information. Check [this whole section of the docs](https://pyinstaller.org/en/stable/runtime-information.html) – buran Apr 05 '23 at 15:59
  • Okay @buran I have now run the script without bundling it together using pyinstaller, and I still get the same error: File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\docx\opc\phys_pkg.py", line 30, in __new__ raise PackageNotFoundError( docx.opc.exceptions.PackageNotFoundError: Package not found at 'qvr_templates\010AHU.docx' It as if the file does not exist (which it does) or it does not have permission to access it maybe? – Ruben Adriaanse Apr 06 '23 at 07:46
  • @buran Additionally I've made a simple python script to use docx to just read in a document, and change the names of the .docx files: ``` from docx import Document import os template_path = "C:\Programming\Envisage2.0\qvr_templates\AHU001.docx" print(template_path) template_document = Document(template_path) ``` And I still get a not found error. I'm running this script on its own, without any compiling by pyinstaller – Ruben Adriaanse Apr 06 '23 at 08:05
  • Please [edit] your question with the information from your last two comments, formatted as code. Please do not put extensive code or error messages in comments. Anything relevant to the question should be put in the question itself, and referenced in comments if needed. – MattDMo Apr 06 '23 at 16:28

1 Answers1

0

I managed to solve it, found the answer here:

https://stackoverflow.com/a/73289012/15163302

The two documents I am trying to combine are empty, and thats why its throwing an error!

As soon as I put something in the documents, it worked.

benson23
  • 16,369
  • 9
  • 19
  • 38