【问题标题】:How to insert pictures into a docx file on the same horizontal line using python-docx? [duplicate]如何使用 python-docx 将图片插入到同一水平线上的 docx 文件中? [复制]
【发布时间】:2019-01-16 15:53:04
【问题描述】:

我可以使用下面的代码将多个图像插入到 Word 文档中,但它们会出现在彼此下方。有没有办法将它们插入到彼此相邻的位置?

from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

document.add_picture(image_filepath, width=Inches(1.25))
document.add_picture(image_filepath, width=Inches(1.25))

document.save(doc_filepath)

【问题讨论】:

    标签: python ms-word docx


    【解决方案1】:

    一种方法是创建透明表格并将图像添加到单元格:

    from docx import Document
    
    document = Document()
    tables = document.tables
    table = document.add_table(rows=1, cols=2)
    row_cells = table.add_row().cells
    
    for i, image in enumerate(['image1.jpg', 'image2.jpg']):
        paragraph = row_cells[i].paragraphs[0]
        run = paragraph.add_run()
        run.add_picture(image)
    document.save('doc.docx')
    

    这就是你会得到的例子:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-13
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2016-10-30
      • 1970-01-01
      相关资源
      最近更新 更多