python如何在word中存儲(chǔ)本地圖片
安裝: pip install python-docx
from docx import Documentfrom docx.shared import Inches string = ’文字內(nèi)容’images = ’1.jpg’ # 保存在本地的圖片doc = Document() # doc對(duì)象doc.add_paragraph(string) # 添加文字doc.add_picture(images, width=Inches(2)) # 添加圖, 設(shè)置寬度doc.save(’word文檔.docx’) # 保存路徑
執(zhí)行結(jié)果: 本地生成了一個(gè)Word文檔, 打開(kāi)之后.
但是有時(shí)添加圖片會(huì)產(chǎn)生識(shí)別異常:
這是因?yàn)閳D片的格式問(wèn)題, 對(duì)比一下 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) # 打開(kāi)圖片 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的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法3. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)4. python 利用toapi庫(kù)自動(dòng)生成api5. Spring如何使用xml創(chuàng)建bean對(duì)象6. Android Studio設(shè)置顏色拾色器工具Color Picker教程7. python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼8. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法9. Java程序的編碼規(guī)范(6)10. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例
