- 下载python-docx
这个在setting里我没找到,所以是在terminal里手动pip install python-docx的
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1763f509ddb4db86e045498285adc61b.png)
- python程序
from docx import Document
from docx.shared import Inches
import os
from PIL import Image
fold='C:\\Users\yyh\Desktop\homework_tmp\pic'
for root, dirs, pics in os.walk(fold):
doc=Document()
for i in range(0,len(pics)):
filepath = 'C:\\Users\yyh\Desktop\homework_tmp\pic\\'+pics[i]
try:
doc.add_picture(filepath,width=Inches(3),height=Inches(4))
except Exception:
pic_tmp=Image.open(filepath)
pic_tmp.save(pic_tmp)
doc.add_picture(filepath, width=Inches(3),height=Inches(4))
doc.save('C:\\Users\yyh\Desktop\homework_tmp\p_pic.docx')
print("pic", i+1, "successfully added.")
- 结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5585e9eaa855cdc8bbf06c5e36f25ed9.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb1aa590ded8df905397415beee4e2f3.png)