首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从url向python docxtpl添加图像

如何从url向python docxtpl添加图像
EN

Stack Overflow用户
提问于 2021-12-01 12:21:01
回答 1查看 227关注 0票数 2

我有一个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'

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-09 12:14:23

我找到了该怎么做

在我的模型中,我添加了以下内容(我在一个名为ImageURL的char字段中使用了一个带有图像urls的模型)

代码语言:javascript
复制
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类中使用此属性,并且它可以工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70184105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档