推荐学习书目
› 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 · Dec 26, 2019 · 4175 views
    This topic created in 2468 days ago, the information mentioned may be changed or developed.

    先贴代码

    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.docx和template_without_line.docx,template.docx里面有四条直线,template_without_line.docx有两条直线,其他的都一样。

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

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

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   856 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    ♥ Do have faith in what you're doing.