如何在一个word文件(.docx)中将两张图片放在同一层python

How to put two pictures on the same level in a word file (.docx) python

我想把标志和条形码放在同一层,而不是放在上面 other.The 标志应该放在最左边,条形码应该放在单词的最右边 file.Here 是我的代码,谢谢:

import uuid 
import pandas as pd
import pyqrcode 
from docx import Document
from docx.shared import Inches


qr=pyqrcode.create(str(uuid.uuid4()).replace('-',''))
qr.png('somecode.png')

df=pd.DataFrame(pd.read_excel('Komplette- 
GastAccountFHZugangsdatenFertig.xlsx'))

Attributes=['Name', 'Vorname ', 'Geschlecht', 'Adresse (in Marokko)',
       'Telefonnummer', 'E-Mailadresse', 'Studiengang', 'Semester']


document = Document()

document.add_heading('Informationen.',level=0)

document.add_picture('Informatik_Logo.png',height=Inches(1.0))


p = document.add_paragraph()
r = p.add_run()


p_format=p.paragraph_format

p_format.left_indent=Inches(4.5)
r.add_picture('somecode.png',width=Inches(1.0))


table=document.add_table(len(Attributes),2,style='LightGrid-Accent1')


for i in range(len(Attributes)):
        row=table.rows[i]
        row.cells[0].text=Attributes[i]
        Infos=df[Attributes[i]]
        string=str(Infos[49])
        row.cells[1].text=string



document.save('sample.docx')

据我在the documentation中看到的,python-docx目前只支持inline图片,不支持floating图片,这意味着您只能获得当前的外观。来自文档:

At the time of writing, python-docx only supports inline pictures. Floating pictures can be added. If you have an active use case, submit a feature request on the issue tracker. The Document.add_picture() method adds a specified picture to the end of the document in a paragraph of its own.

根据最后一句话,我认为您目前正在尝试做的事情是不可能的。解决方法可能是插入一行两列的 table,并在每个单元格中插入一个图像。

我可以帮助你,只需在你的代码中添加一句话

r.add_picture('somecode.png',width=Inches(2.0))

就是这样。多写一句time.and 如果你想在同一个级别多一张图片,你需要做的就是加一句。我试过了,对我有用。

有我的测试代码

dc = Document()

run = dc.add_paragraph().add_run()
run.add_picture("./picture/danilise.jpg", width=Inches(1.1))
run.add_picture("./picture/danilise.jpg", width=Inches(1.3))
run.add_picture("./picture/danilise.jpg", width=Inches(1.5))

dc.save("test1.docx")