我有一个docxtpl文档,但是我需要从url而不是本地磁盘向InlineImage添加图像。
我在用Django
因此,这是可行的(文档是一个DocxTemplate):
InlineImage(document,'C:/Users/sande/Pictures/myimage.png')
但我该怎么做呢?
InlineImage(document,'https://files-cdn.myhost.net/3255/ddef9d07-0a6b-4f54-801a-c016e6d41885/myimage.png')
image_descriptior不接受url。
Exception has occurred: OSError [Errno 22] Invalid argument:'https://files-cdn.myhost.net/3255/ddef9d07-0a6b-4f54-801a-c016e6d41885/myimage.png'
发布于 2021-12-09 12:14:23
我找到了该怎么做
在我的模型中,我添加了以下内容(我在一个名为ImageURL的char字段中使用了一个带有图像urls的模型)
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您可以在docxtpl的inlineimage类中使用此属性,并且它可以工作。
https://stackoverflow.com/questions/70184105
复制相似问题