在 python-docx 的同一行中添加两个图像

add two images in same line in python-docx

我正在尝试在 docx 文件中添加两个图像。 图片应该是一左一右。 使用下面的代码后,图像位置像我想要的那样左右工作,但它们不在我想要的同一行上。 一个在上面,其他在那个图像下面。

我试过 WD_ALIGN_PARAGRAPH.RIGHT 但我没有得到我想要的结果。

## Image for Left Side
my_img = document.add_picture(i,width=Inches(0.8),height=Inches(0.8))
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT

## Image for Right Side
my_img2 = document.add_picture(i,width=Inches(0.8),height=Inches(0.8))
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

请帮帮我,我希望两张图片在同一行上,就像两张图片一样,它们之间的 space 很小。

使用 Run.add_picture() 而不是 Paragraph.add_picture()。这将允许在同一段落中使用多个图像,如果它们都在页边距内,将导致并排图像:

paragraph = document.add_paragraph()
run = paragraph.add_run()
run.add_picture(...)
run_2 = paragraph.add_run()
run_2.add_picture(...)

就对齐而言,在使用段落时,插入制表符可能是最可靠的。另一种方法是添加 table 并将图像放在并排的单元格中。