Python docxtpl利用InlineImage实现word里添加图片内容

Python docxtpl利用InlineImage实现word里添加图片内容,第1张

from docxtpl import DocxTemplate,InlineImage,RichText
import docxtpl
from docx.shared import Mm


# DOCX生成下载视图
# 提供DOCX的下载服务

# path :路径信息
# person : 传入的具体人员数据信息
# dataPath:传入的图片地址

def render_template_docx(path, person,dataPath):
    gugan = DocxTemplate(f"{path}gugan.docx")  # 打开对应的docx文件
    context = {}
    image_path = ""
    insert_image = ""
    # 检查人员数据库的某个人的图片地址不为空
    if (person.photo_src != None):
        image_path = dataPath+person.photo_src
        insert_image = docxtpl.InlineImage(gugan, image_path, width=Mm(50),height=Mm(70))
    context = {
        'name': person.name,
        "img": insert_image,
        'sex': person.sex,
        'nation': person.nation,
        'birth': person.birth,
        'status': person.status,
        'contact': person.contact,
        'job_title': person.job_title,
        'company': person.company,
        'address': person.address,
        'introduce': person.introduce,
        'awards': person.awards,
        'production_src': person.production_src,
        'skill': person.skill
    }
    gugan.render(context)  # 存取context里的信息
    gugan.save(f"{path}{person.id}.docx")
    # 保存到path路径,准备返回给用户下载,以用户的id进行命名 *** 作

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/568212.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-09
下一篇2022-04-09

发表评论

登录后才能评论

评论列表(0条)

    保存