python-docx-template icon indicating copy to clipboard operation
python-docx-template copied to clipboard

Cannot use replace_pic

Open jcomish opened this issue 4 years ago • 11 comments

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.

jcomish avatar Jul 08 '21 15:07 jcomish

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

gaetan1903 avatar Jul 09 '21 18:07 gaetan1903

Please, could you provide a fully runnable test case (python code + docx template + other files if needed).

elapouya avatar Jul 16 '21 08:07 elapouya

@jcomish I have the same problem. Any news on this?

thielena avatar Aug 31 '21 09:08 thielena

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.

thielena avatar Aug 31 '21 10:08 thielena

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 ,

gaetan1903 avatar Oct 20 '21 19:10 gaetan1903

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)

FreshMedlar avatar Jan 19 '22 21:01 FreshMedlar

i have the same problem, but how can i find out under which name my picture been saved in docx template?

mureedd avatar May 16 '22 09:05 mureedd

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.

elapouya avatar May 21 '22 12:05 elapouya

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.

kit57 avatar Jun 15 '22 09:06 kit57

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

akinaTRUENO avatar Feb 08 '23 02:02 akinaTRUENO

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')

iigor93 avatar Mar 06 '23 11:03 iigor93