V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
walle
V2EX  ›  Python

有没有用过 docxtpl 或者对 Word 比较熟的大佬?渲染 Word 的时候出现一个特别奇怪的问题。

  •  
  •   walle · 2019-12-26 20:28:18 +08:00 · 3299 次点击
    这是一个创建于 1584 天前的主题,其中的信息可能已经有所发展或是发生改变。

    先贴代码

    import jinja2
    from docxtpl import InlineImage, DocxTemplate
    from docx.shared import Mm
    
    
    # template = DocxTemplate('template.docx')
    template = DocxTemplate('template_without_line.docx')
    
    
    def get_inline_image(template):
        inline_image = {}
        inline_image['img1'] = InlineImage(template, 'img1.png', width=Mm(146.4))
        inline_image['img2'] = InlineImage(template, 'img2.png', width=Mm(146.4))
        inline_image['img3'] = InlineImage(template, 'img3.png', width=Mm(146.4))
        inline_image['img4'] = InlineImage(template, 'img4.png', width=Mm(146.4))
     
        return inline_image
    
    
    data = get_inline_image(template)
    jinja_env = jinja2.Environment(autoescape=True)
    
    template.render(data, jinja_env)
    
    template.save('result.docx')
    

    我创建了两个模板,template.docxtemplate_without_line.docx,template.docx里面有四条直线,template_without_line.docx有两条直线,其他的都一样。

    但是用jinja2渲染以后发现通过template_without_line.docx生成的 Word 是可以打开的,template.docx生成的打开就会报“发现无法读取的内容”这个错误(如果信任文档仍然是可以打开的)。

    只是两个直线的差距,会对渲染造成什么影响?

    模板和代码都已经提交到GitHub上面了。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:54 · PVG 06:54 · LAX 15:54 · JFK 18:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.