python如何在word中存儲本地圖片
安裝: 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é)果: 本地生成了一個Word文檔, 打開之后.
但是有時添加圖片會產(chǎn)生識別異常:
這是因為圖片的格式問題, 對比一下 0.jpg 和 1.jpg的二進制數(shù)據(jù), 添加0.jpg會異常, 1.jpg則不會.
圖片格式轉(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中存儲本地圖片的詳細內(nèi)容,更多關(guān)于python本地圖片存儲Word的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JS中map和parseInt的用法詳解2. 爬取今日頭條Ajax請求3. ASP.NET Core按用戶等級授權(quán)的方法4. HTML DOM setInterval和clearInterval方法案例詳解5. ASP常用日期格式化函數(shù) FormatDate()6. 詳解JSP 內(nèi)置對象request常見用法7. webpack高級配置與優(yōu)化詳解8. HTML <!DOCTYPE> 標(biāo)簽9. JavaScrip簡單數(shù)據(jù)類型隱式轉(zhuǎn)換的實現(xiàn)10. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享
