Python xlwings插入Excel圖片的實(shí)現(xiàn)方法
測(cè)試圖片
一、相對(duì)路徑(報(bào)錯(cuò))
使用相對(duì)路徑插入會(huì)報(bào)錯(cuò)(確認(rèn)路徑正確無誤)
import xlwings as xw wb = xw.Book() sht = wb.sheets['Sheet1'] sht.pictures.add('1.jpg') # 使用相對(duì)路徑會(huì)報(bào)錯(cuò) wb.save('test.xlsx') wb.close()
File "<COMObject <unknown>>", line 5, in AddPicture
pywintypes.com_error: (-2147352567, '發(fā)生意外。', (0, None, '未找到指定文件。', None, 0, -2146827284), None)
二、絕對(duì)路徑
改為絕對(duì)路徑即可成功插入
import os import xlwings as xw wb = xw.Book() sht = wb.sheets['Sheet1'] # sht.pictures.add('1.jpg') # 使用相對(duì)路徑會(huì)報(bào)錯(cuò) sht.pictures.add(os.path.join(os.getcwd(), '1.jpg')) wb.save('test.xlsx') wb.close()
三、指定位置和大小
函數(shù)原型add(image, link_to_file=False, save_with_document=True, left=0, top=0, width=None, height=None, name=None, update=False)
import os import xlwings as xw wb = xw.Book() sht = wb.sheets['Sheet1'] fileName = os.path.join(os.getcwd(), '1.jpg') sht.pictures.add(fileName, left=sht.range('B5').left, top=sht.range('B5').top, width=100, height=100) wb.save('test.xlsx') wb.close()
指定圖片位置為B5
單元格的左上角,圖片像素為100×100
四、居中插入
新建Excel文件test.xlsx
,設(shè)置列寬20行高100
import os import xlwings as xw wb = xw.Book('test.xlsx') # 打開已存在的Excel文件 sht = wb.sheets['Sheet1'] rng = sht.range('B2') # 目標(biāo)單元格 fileName = os.path.join(os.getcwd(), '1.jpg') width, height = 80, 80 # 指定圖片大小 left = rng.left + (rng.width - width) / 2 # 居中 top = rng.top + (rng.height - height) / 2 sht.pictures.add(fileName, left=left, top=top, width=width, height=height) wb.save() wb.close()
智能居中插入
1.jpg
寬 × 高 = 188 × 282
2.jpg
寬 × 高 = 200 × 153
import os import xlwings as xw from PIL import Image def add_center(sht, target, filePath, match=False, width=None, height=None, column_width=None, row_height=None): '''Excel智能居中插入圖片 優(yōu)先級(jí):match > width & height > column_width & row_height 建議使用column_width或row_height,定義單元格最大寬或高 :param sht: 工作表 :param target: 目標(biāo)單元格,字符串,如'A1' :param filePath: 圖片絕對(duì)路徑 :param width: 圖片寬度 :param height: 圖片高度 :param column_width: 單元格最大寬度,默認(rèn)100像素,0 <= column_width <= 1557.285 :param row_height: 單元格最大高度,默認(rèn)75像素,0 <= row_height <= 409.5 :param match: 絕對(duì)匹配原圖寬高,最大寬度1557.285,最大高度409.5 ''' unit_width = 6.107 # Excel默認(rèn)列寬與像素的比 rng = sht.range(target) # 目標(biāo)單元格 name = os.path.basename(filePath) # 文件名 _width, _height = Image.open(filePath).size # 原圖片寬高 NOT_SET = True # 未設(shè)置單元格寬高 # match if match: # 絕對(duì)匹配圖像 width, height = _width, _height else: # 不絕對(duì)匹配圖像 # width & height if width or height: if not height: # 指定了寬,等比計(jì)算高 height = width / _width * _height if not width: # 指定了高,等比計(jì)算寬 width = height / _height * _width else: # column_width & row_height if column_width and row_height: # 同時(shí)指定單元格最大寬高 width = row_height / _height * _width # 根據(jù)單元格最大高度假設(shè)寬 height = column_width / _width * _height # 根據(jù)單元格最大寬度假設(shè)高 area_width = column_width * height # 假設(shè)寬優(yōu)先的面積 area_height = row_height * width # 假設(shè)高優(yōu)先的面積 if area_width > area_height: width = column_width else: height = row_height elif not column_width and not row_height: # 均無指定單元格最大寬高 column_width = 100 row_height = 75 rng.column_width = column_width / unit_width # 更新當(dāng)前寬度 rng.row_height = row_height # 更新當(dāng)前高度 NOT_SET = False width = row_height / _height * _width # 根據(jù)單元格最大高度假設(shè)寬 height = column_width / _width * _height # 根據(jù)單元格最大寬度假設(shè)高 area_width = column_width * height # 假設(shè)寬優(yōu)先的面積 area_height = row_height * width # 假設(shè)高優(yōu)先的面積 if area_width > area_height: height = row_height else: width = column_width else: width = row_height / _height * _width if row_height else column_width # 僅設(shè)了單元格最大寬度 height = column_width / _width * _height if column_width else row_height # 僅設(shè)了單元格最大高度 assert 0 <= width / unit_width <= 255 assert 0 <= height <= 409.5 if NOT_SET: rng.column_width = width / unit_width # 更新當(dāng)前寬度 rng.row_height = height # 更新當(dāng)前高度 left = rng.left + (rng.width - width) / 2 # 居中 top = rng.top + (rng.height - height) / 2 try: sht.pictures.add(filePath, left=left, top=top, width=width, height=height, scale=None, name=name) except Exception: # 已有同名圖片,采用默認(rèn)命名 pass if __name__ == '__main__': wb = xw.Book() sht = wb.sheets['Sheet1'] filePath = os.path.join(os.getcwd(), '1.jpg') filePath2 = os.path.join(os.getcwd(), '2.jpg') add_center(sht, 'A1', filePath) # 默認(rèn)值 add_center(sht, 'B2', filePath2) # 默認(rèn)值 add_center(sht, 'C3', filePath, match=True) # 絕對(duì)匹配圖片寬高 add_center(sht, 'D4', filePath, width=100) # 圖片寬度為100像素 add_center(sht, 'E5', filePath, height=100) # 圖片高度為100像素 add_center(sht, 'F6', filePath, width=100, height=100) # 圖片高度為100像素 add_center(sht, 'G7', filePath, column_width=100) # 單元格最大寬度為100像素 add_center(sht, 'H8', filePath, row_height=100) # 單元格最大寬度為100像素 add_center(sht, 'I9', filePath, column_width=100, row_height=100) # 單元格最大高度或?qū)挾葹?00像素
效果
unit_width = 6.107 # Excel默認(rèn)列寬與像素的比
這個(gè)值估計(jì)與不同機(jī)器、分辨率有關(guān),在5.7-6.2之間
遇到的坑
報(bào)錯(cuò) pywintypes.com_error: (-2147352567, '發(fā)生意外。', (0, None, '未找到指定文件。', None, 0, -2146827284), None)
對(duì)路徑使用os.path.abspath()
參考文獻(xiàn)
到此這篇關(guān)于Python xlwings插入Excel圖片的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Python xlwings插入圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Django ForeignKey 反向查詢中filter和_set的效率對(duì)比詳解
今天小編就為大家分享一篇關(guān)于Django ForeignKey 反向查詢中filter和_set的效率對(duì)比詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12使用Python獲取愛奇藝電視劇彈幕數(shù)據(jù)的示例代碼
這篇文章主要介紹了用Python獲取愛奇藝電視劇彈幕數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01python在CMD界面讀取excel所有數(shù)據(jù)的示例
這篇文章主要介紹了python在CMD界面讀取excel所有數(shù)據(jù),幫助大家更好的利用python辦公,感興趣的朋友可以了解下2020-09-09解決Python安裝時(shí)報(bào)缺少DLL問題【兩種解決方法】
這篇文章主要介紹了解決Python安裝時(shí)報(bào)缺少DLL問題,本文通過兩種方法給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07Django框架會(huì)話技術(shù)實(shí)例分析【Cookie與Session】
這篇文章主要介紹了Django框架會(huì)話技術(shù),結(jié)合實(shí)例形式分析了Django框架Cookie與Session相關(guān)使用技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05基于PyQt5制作Excel數(shù)據(jù)分組匯總器
這篇文章主要介紹了基于PyQt5制作的一個(gè)小工具:Excel數(shù)據(jù)分組匯總器。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01Python數(shù)據(jù)報(bào)表之Excel操作模塊用法分析
這篇文章主要介紹了Python數(shù)據(jù)報(bào)表之Excel操作模塊用法,結(jié)合實(shí)例形式分析了XlsxWriter模塊的功能及簡(jiǎn)單使用方法,需要的朋友可以參考下2019-03-03