- Python XlsxWriter 教程
- Python XlsxWriter - 首页
- Python XlsxWriter - 概述
- Python XlsxWriter - 环境设置
- Python XlsxWriter - Hello World
- Python XlsxWriter - 重要类
- Python XlsxWriter - 单元格表示法和范围
- Python XlsxWriter - 定义名称
- Python XlsxWriter - 公式和函数
- Python XlsxWriter - 日期和时间
- Python XlsxWriter - 表格
- Python XlsxWriter - 应用筛选器
- Python XlsxWriter - 字体和颜色
- Python XlsxWriter - 数字格式
- Python XlsxWriter - 边框
- Python XlsxWriter - 超链接
- Python XlsxWriter - 条件格式化
- Python XlsxWriter - 添加图表
- Python XlsxWriter - 图表格式化
- Python XlsxWriter - 图表图例
- Python XlsxWriter - 条形图
- Python XlsxWriter - 折线图
- Python XlsxWriter - 饼图
- Python XlsxWriter - Sparklines (迷你图)
- Python XlsxWriter - 数据验证
- Python XlsxWriter - 大纲和分组
- Python XlsxWriter - 冻结和拆分窗格
- Python XlsxWriter - 隐藏/保护工作表
- Python XlsxWriter - 文本框
- Python XlsxWriter - 插入图片
- Python XlsxWriter - 页面设置
- Python XlsxWriter - 页眉和页脚
- Python XlsxWriter - 单元格批注
- Python XlsxWriter - 使用 Pandas
- Python XlsxWriter - VBA 宏
- Python XlsxWriter 有用资源
- Python XlsxWriter - 快速指南
- Python XlsxWriter - 有用资源
- Python XlsxWriter - 讨论
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_scale 和 y_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()
输出
以下是结果工作表的视图:
广告