Python XlsxWriter - 插入圖片



藉助insert_image() 方法,可以在工作表的特定單元格位置插入影像物件。基本上,您必須使用任何型別的表示法指定單元格位置以及要插入的影像。

worksheet.insert_image('C5', 'logo.png')

insert_image() 方法在字典中接受以下可選引數。

引數 預設值
'x_offset' 0,
'y_offset' 0,
'x_scale' 1,
'y_scale' 1,
'object_position' 2,
'image_data' None
'url' None
'description' None
'decorative' False

偏移值以畫素為單位。x_scaley_scale 引數用於水平和垂直縮放影像。

image_data 引數用於新增io.BytesIO 格式的記憶體位元組流。

示例

以下程式從當前資料夾中的檔案提取影像資料,並將其用作image_data 引數的值。

from io import BytesIO
import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

filename = 'logo.png'

file = open(filename, 'rb')
data = BytesIO(file.read())
file.close()

worksheet.insert_image('C5', filename, {'image_data': data})

workbook.close()

輸出

以下是結果工作表的檢視:

Insert Image
廣告
© . All rights reserved.