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

Ash*_*ani 3 python docx python-3.x pandas 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
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

sca*_*nny 5

使用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(...)
Run Code Online (Sandbox Code Playgroud)

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