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
king625
V2EX  ›  Python

Python 如何在 docx 的指定位置插入图片,并设置图片大小?

  •  
  •   king625 · 2018-05-10 18:19:05 +08:00 · 9116 次点击
    这是一个创建于 2231 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要在 docx 的指定位置插入结果图片,并且设置图片大小,最好可以在 table 的一个 cell 中插入图片,但是使用 python-docx 尝试,好像插入时只能插入在文件末端?

    qile1
        1
    qile1  
       2018-05-10 22:22:53 +08:00 via Android
    听说 docx 是个 zip 压缩了 xml 文件的文件,你先插入一个图片作为模板文件,然后替换图片可以不?
    king625
        2
    king625  
    OP
       2018-05-11 09:02:02 +08:00
    @qile1 谢谢, 后来翻了一下 python-docx 的 Issues,发现大神之前发布了解决方法,链接如下:
    https://github.com/python-openxml/python-docx/issues/10

    代码如下:
    paragraph = cell.paragraphs[0]
    run = paragraph.add_run()
    run.add_picture('image.png')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1444 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:48 · PVG 07:48 · LAX 16:48 · JFK 19:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.