Python XlsxWriter 插入图像
可以在工作表的某个单元格位置插入图像对象,借助 插入图像() 方法。基本上,你必须使用任何类型的符号和要插入的图像来指定单元格的位置。
worksheet.insert_image('C5', 'logo.png')
The 插入图像() 方法采用字典中的以下可选参数。
范围 | Default |
---|---|
'x_offset' | 0, |
'y_offset' | 0, |
'x_scale' | 1, |
'y_scale' | 1, |
“对象位置” | 2, |
'图像数据' | None |
'url' | None |
'描述' | None |
'装饰性的' | False |
偏移值以像素为单位。这 x_scale and y_scale 参数用于水平和垂直缩放图像。
The 图像数据 参数用于在内存中添加一个字节流 io.BytesIO format.
例子
以下程序从当前文件夹中的文件中提取图像数据,并使用 is 作为值 图像数据 范围。
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()
输出
这是生成的工作表的视图: