1. 添加图片
函数: add_picture(图片文件,width=宽, height=高)
如果只指定一个宽或高,则按比例缩放。
1.1 文档直接添加图片
from docx import Document
from docx.shared import Cm
doc = Document()
doc.add_picture("images/2.jpg", width=Cm(8))
doc.save("1.docx")
1.2 段落中添加图片
段落中的Run可以添加图片。
from docx import Document
from docx.shared import Cm
doc = Document()
doc.add_paragraph().add_run().add_picture("images/3.jpg",height=Cm(8))
doc.save("1.docx")
1.3 表格中添加图片
先在表格的单元格中添加一个Run,之后添加图片:
from docx import Document
from docx.shared import Cm
doc = Document()
table = doc.add_table(2, 2)
cell = table.cell(0, 0)
cell.paragraphs[0].add_run().add_picture("images/2.jpg", width=Cm(5))
cell = table.cell(0, 1)
cell.paragraphs[0].add_run().add_picture("images/2.jpg", width=Cm(5))
doc.save("1.docx")
标签:docx,Cm,python,doc,cell,add,Document
From: https://www.cnblogs.com/three-sheep/p/17508278.html