python如何在word中存儲(chǔ)本地圖片
安裝: pip install python-docx
from docx import Documentfrom docx.shared import Inches string = ’文字內(nèi)容’images = ’1.jpg’ # 保存在本地的圖片doc = Document() # doc對象doc.add_paragraph(string) # 添加文字doc.add_picture(images, width=Inches(2)) # 添加圖, 設(shè)置寬度doc.save(’word文檔.docx’) # 保存路徑
執(zhí)行結(jié)果: 本地生成了一個(gè)Word文檔, 打開之后.

但是有時(shí)添加圖片會(huì)產(chǎn)生識(shí)別異常:

這是因?yàn)閳D片的格式問題, 對比一下 0.jpg 和 1.jpg的二進(jìn)制數(shù)據(jù), 添加0.jpg會(huì)異常, 1.jpg則不會(huì).

圖片格式轉(zhuǎn)換
from docx import Documentfrom docx.shared import Inchesfrom PIL import Image string = ’文字內(nèi)容’images = ’0.jpg’ # 保存在本地的圖片doc = Document()doc.add_paragraph(string) # 添加文字 try: doc.add_picture(images, width=Inches(2)) # 添加圖, 設(shè)置寬度except Exception: jpg_ima = Image.open(images) # 打開圖片 jpg_ima.save(’0.jpg’) # 保存新的圖片 doc.add_picture(images, width=Inches(2)) # 添加圖, 設(shè)置寬度 doc.save(’word文檔.docx’) # 保存路徑
結(jié)果就和前面一樣了:

以上就是python如何在word中存儲(chǔ)本地圖片的詳細(xì)內(nèi)容,更多關(guān)于python本地圖片存儲(chǔ)Word的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. javascript實(shí)現(xiàn)貪吃蛇小練習(xí)2. Java commons-httpclient如果實(shí)現(xiàn)get及post請求3. Vue 實(shí)現(xiàn)對quill-editor組件中的工具欄添加title4. JavaScript實(shí)現(xiàn)簡單的彈窗效果5. Vue的props父傳子的示例代碼6. PHP單件模式和命令鏈模式的基礎(chǔ)知識(shí)7. 我所理解的JavaScript中的this指向8. 一文帶你徹底理解Java序列化和反序列化9. PHP利用curl發(fā)送HTTP請求的實(shí)例代碼10. PHP驗(yàn)證碼工具-Securimage

網(wǎng)公網(wǎng)安備