elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.91k stars 378 forks source link

replace_embedded: embedded_dummy.docx MUST exist in the template directory doesn't work #457

Closed tlaloc911 closed 1 year ago

tlaloc911 commented 1 year ago

Describe the bug

Documentation says "embedded_dummy.docx MUST exist in the template directory when rendering and saving the generated docx" but the real behavior is embedded_dummy.docx MUST exist in the current directory.

To Reproduce

I exec my script from a cmd console on my working directory. At the beginnig my dummy files where on my working directory but I decided to move them to the templates directory in order to keep it cleaner.

This works if dummy is on my working directory

if os.path.exists(template + "_attach.docx"):
        DocxRef.replace_embedded("dummy.docx",template + "_attach.docx")

When I move the file to the template directory, it broke

Traceback (most recent call last):
  File "C:\Users\xxx\script.py", line 170, in <module>
    Generate("SolutionSpecification",directory_path, context)
  File "C:\Users\xxx\script.py", line 52, in Generate
    DocxRef.replace_embedded("dummy.docx",template + "_attach.docx")
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\docxtpl\template.py", line 540, in replace_embedded
    crc = self.get_file_crc(src_file)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\docxtpl\template.py", line 472, in get_file_crc
    with open(file_obj, 'rb') as fh:
FileNotFoundError: [Errno 2] No such file or directory: 'dummy.docx'

I had to change my code and it worked using replace_zipname instead of replace_embedded

if os.path.exists(template + "_attach.docx"):
        DocxRef.replace_zipname("word/embeddings/Microsoft_Word_Document.docx",template + "_attach.docx")

Expected behavior

In both cases I could replace a docx attached but I expect replace_embedded behaves the same way than replace_zipname regarding with dummy file location according with documentation embedded_dummy.docx MUST exist in the template directory doesn't work.

elapouya commented 1 year ago

What if you use a full path for dummy file ?

Le mer. 21 sept. 2022 à 16:24, tlaloc911 @.***> a écrit :

Describe the bug

Documentation says "embedded_dummy.docx MUST exist in the template directory when rendering and saving the generated docx" but the real behavior is embedded_dummy.docx MUST exist in the current directory. To Reproduce

I exec my script from a cmd console in my working directory. At the beginnig my dummy files where on my working directory but I decided to move them to the templates directory in order to keep it cleaner.

This works if dummy is in working directory

if os.path.exists(template + "_attach.docx"): DocxRef.replace_embedded("dummy.docx",template + "_attach.docx")

When I move the file to the template directory, it broke

Traceback (most recent call last): File "C:\Users\xxx\script.py", line 170, in Generate("SolutionSpecification",directory_path, context) File "C:\Users\xxx\script.py", line 52, in Generate DocxRef.replace_embedded("dummy.docx",template + "_attach.docx") File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\docxtpl\template.py", line 540, in replace_embedded crc = self.get_file_crc(src_file) File "C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\docxtpl\template.py", line 472, in get_file_crc with open(file_obj, 'rb') as fh: FileNotFoundError: [Errno 2] No such file or directory: 'dummy.docx'

I had to change my code and it worked using replace_zipname instead of replace_embedded

if os.path.exists(template + "_attach.docx"): DocxRef.replace_zipname("word/embeddings/Microsoft_Word_Document.docx",template + "_attach.docx")

Expected behavior

In both cases I could replace a docx attached but I expect replace_embedded behaves the same way than replace_zipname regarding with dummy file location according with documentation embedded_dummy.docx MUST exist in the template directory doesn't work.

— Reply to this email directly, view it on GitHub https://github.com/elapouya/python-docx-template/issues/457, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGY33EFG23NI7VS3N637EDV7MLADANCNFSM6AAAAAAQSEBNGY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

tlaloc911 commented 1 year ago

Thanks for your answer @elapouya , it worked well including full path of dummy file using replace_embedded function. It would be nice both functions were consistent.

elapouya commented 1 year ago

Closing as it is obvions to use full path. It is not like replace_zipname which is for zip files.