Cannot use replace_pic
I am trying to replace an image on a document instead of inserting it, since I need the alt text to be populated. The problem is that I trying to use replace_pic on an image inside the document (not header or footer) and it is not finding the image I loaded into the Word Document.
Here is my code:
from docxtpl import DocxTemplate, InlineImage
doc = DocxTemplate("t.docx")
doc.replace_pic('replace.png', 'graph.png')
doc.render(context)
doc.save("generated_doc.docx")
Am I simply using the function incorrectly? I have tried several different file names that were hidden in the doc xml to no avail.
I've the same issue, I fix it by create the template on Word 2013,
i've using word 2019, and it does'nt work.
sorry for my bad english
Please, could you provide a fully runnable test case (python code + docx template + other files if needed).
@jcomish I have the same problem. Any news on this?
After some reverse engineering, I found that Word now uses internal names like name="Graphic 1" for these images. And this is the reason why no more substitution can be done based on the file name.
The solution here would be to set the alternative text for the images in Word and use this instead of the file name as a reference for the replacement.
Same other issue today, I create my doc in Onlyoffice on Linux, and i can't replace pic, so i have to import image with Office 2016 and it's works ,
I had the same issue, thanks to @thlnndrs I figured it out. I opened the docx as a zip and in the document.xml the picture is referred as "Picture 13" and it worked.
doc = DocxTemplate(template)
context = {}
doc.replace_pic('Picture 13', r'C:\path\to\other\image')
doc.render(context)
doc.save(output_file)
i have the same problem, but how can i find out under which name my picture been saved in docx template?
Please try :
tpl.replace_media('dummy_pic.jpg','pic_i_want.jpg')
Both images files must exists. 'dummy_pic.jpg' is used in the docx template but must also exist as a file so docxpl can calculate its checksum and find it automatically in the docx template for replacement.
i have the same problem, but how can i find out under which name my picture been saved in docx template?
You can unzip your .docx file and check inside the word/media subfolders what's the name of your image. @mureedd
Then you can run the tpl.replace_pic('pic_original_name.jpg','pic_replace.jpg')
Edit: I was trying to replace an image from the header and had some problems. Got it solved by using tpl.replace_media('pic_original_name.jpg','pic_replace.jpg') as @elapouya suggested.
I've had the smae issue when using replace_pic method, I get it solved by naming the picture in Alt Text. the demo document template also names the picture the same way
Hi, here is solutions that helps me: If you insert picture to a template like CTRL+C --> CTRL+V, like @FreshMedlar mentioned, you have to find this name in document.xml. But if you use Insert > Pictures > picture on your PC, this picture appear with its original name (you can check in document.xml, it'll be original name).
So: Insert > Pictures > pic_original_name.jpg (NOT ctrl+c -- ctrl+v)
tpl.replace_pic('pic_original_name.jpg','pic_replace.jpg')