I have a docxtpl document, but I need to add images to the InlineImage from a url, not local disk.
I'm using Django
So this works (document is a DocxTemplate):
InlineImage(document,'C:/Users/sande/Pictures/myimage.png')
But how do i do this?:
InlineImage(document,'https://files-cdn.myhost.net/3255/ddef9d07-0a6b-4f54-801a-c016e6d41885/myimage.png')
the image_descriptior doesn't accept a url.:
Exception has occurred: OSError [Errno 22] Invalid argument:'https://files-cdn.myhost.net/3255/ddef9d07-0a6b-4f54-801a-c016e6d41885/myimage.png'
I found out what to do:
In my model I added the following (I use a model with image urls in a char field called ImageURL)
from django.db import models
import requests
import io
class myModel(models.Model)
ImageURL = models.CharField(max_length=250, null=True, blank=True)
@property
def product_image(self):
"Returns the product image"
response = requests.get(self.ImageURL)
image_bytes = io.BytesIO(response.content)
return image_bytes
You can use this property in the inlineimage class of docxtpl and it works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With