elapouya / python-docx-template

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

replace_zipname does not work for embedding CSV #359

Open Thylowz opened 3 years ago

Thylowz commented 3 years ago

In the same way as described for xlsx file, I tried to reproduce the xlsx example with CSV (since it is not an OLE object, it should work in my mind).

from docxtpl import DocxTemplate

tpl = DocxTemplate('embed_csv_tpl.docx')

context = {"name" : "anonymous"}

tpl.replace_zipname("word/embeddings/Feuille_de_calcul_Microsoft_Excel_prenant_en_charge_les_macros.xlsm", "real_csv.csv")

tpl.render(context)
tpl.save('embed_csv_out.docx')

Sadly, I can't get it to work. Maybe I'm missing something but it seems OK to me.

csv_replacement_test.zip

elapouya commented 3 years ago

I think you can only replace a .csv by a .csv. So embed a dummy .csv instead of your .xlsm in your template.

Thylowz commented 3 years ago

As you can see in the archive I attached, it's what I tried to do in the template. I put a dummy.csv, which Word translates into the .xlsm, which explain why I set up replace_zipname the way I did.

I did that because I thought that the behaviour would be the same as the one with excel file. Should I proceed differently?

elapouya commented 3 years ago

I do not know why MSWord transformed the .csv into a .xlsm, but I guess it is waiting now a .xlsm and not a .csv I am afraid there is no solution for that. May be you could try to put your .csv into a .zip and use a dummy .zip in template.

Thylowz commented 3 years ago

Sadly I do not have that much "flexibility". Do dummy.zip work though? I thought it didn't due to the way OLE object are handled by MSWord?

I'll probably try by converting csv to xlsx and put dummy xlsx.