insert_image(row, col, image[, options])
在工作表单元格中插入一张图片。
参数:
这种方法可用于向工作表插入图片。图片可以是PNG, JPEG或者BMP格式的:
worksheet.insert_image('B2', 'python.png')
文件路径可与图片文件名一起指定:
worksheet1.insert_image('B10', '../images/python.png')
worksheet2.insert_image('B20', r'c:\images\python.png')
insert_image()方法接受字典形式的可选参数来定位和缩放图片。可用的参数和它们的默认值有:
{
'x_offset': 0,
'y_offset': 0,
'x_scale': 1,
'y_scale': 1,
'url': None,
'tip': None,
'image_data': None,
'positioning': None,
}
offset的值是以像素为单位的:
worksheet1.insert_image('B2', 'python.png', {'x_offset': 15, 'y_offset': 10'})
offset的值可以大于基础单元格的宽度与高度。如果想将两个及以上的图片相对于同一单元格对齐,这有时会很有用。
x_scale和y_scale参数可以用于水平及垂直的缩放图片:
worksheet.insert_image('B3', 'python.png', {'x_scale': 0.5, 'y_scale': 0.5})
url
参数可以为图片添加超链接/url,
tip
参数为含有超链接的图片提供可选的鼠标悬停时的提示信息:
worksheet.insert_image('B4', 'python.png', {'url': 'http://python.org'})
image_data参数用于在io.BytesIO中添加内存中的字节流:
worksheet.insert_image('B5', 'python.png', {'image_data': image_data})
这通常用来从URL中添加图片:
url = 'http://python.org/logo.png'
image_data = io.BytesIO(urllibs.urlopen(url).read())
worksheet.insert_image('B5', url, {'image_data': image_data})
当使用
image_data参数时文件名必须传递到insert_image()因为这是Excel要求的。在前面的例子中文件名是从URL字符串中提取的。
positioning参数可以用来控制图片对象的位置:
worksheet.insert_image('B3', 'python.png', {'positioning': 1})
postioning有以下允许的值:
1.移动和调整单元格的大小
2.移动但不调整单元格的大小(默认)
3.不移动或调整单元格的大小
如果由于字体大小大于默认字体大小或者打开了文本换行而导致更改了默认行高,则图片的缩放可能会受到影响。
如果它与插入的图像交叉,你应该使用set_row()方法明确的设定行高来避免此问题。
BMP图片只向后兼容。由于BMP图片不能压缩,通常情况下最好避免使用BMP图片。如果使用BMP图片,必须是24bit、true color、bitmap的。
这是一个向工作表插入图片的例子。
##############################################################################
#
# An example of inserting images into a worksheet using the XlsxWriter
# Python module.
#
# Copyright 2013-2017, John McNamara, [email protected]
#
import xlsxwriter
# 创建一个新Excel文件并添加一个工作表。
workbook = xlsxwriter.Workbook('images.xlsx')
worksheet = workbook.add_worksheet()
# 加宽第一列使文本更清晰。
worksheet.set_column('A:A', 30)
# 插入一张图片。
worksheet.write('A2', '向单元格插入一张图片:')
worksheet.insert_image('B2', 'python.png')
# 插入一张位偏移图片。
worksheet.write('A12', '插入一张位偏移图片:')
worksheet.insert_image('B12', 'python.png', {'x_offset': 15, 'y_offset': 10})
# 插入一张缩放了的图片。
worksheet.write('A23', '插入一张缩放了的图片:')
worksheet.insert_image('B23', 'python.png', {'x_scale': 0.5, 'y_scale': 0.5})
workbook.close()
##############################################################################
#
# An example of inserting images from a Python BytesIO byte stream into a
# worksheet using the XlsxWriter module.
#
# Copyright 2013-2017, John McNamara, [email protected]
#
# 导入字节流处理器(byte stream handler.)
from io import BytesIO
# 从Python 2或3之一导入urlopen()
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
import xlsxwriter
# 创建一个工作簿并添加一个工作表。
workbook = xlsxwriter.Workbook('images_bytesio.xlsx')
worksheet = workbook.add_worksheet()
# 从远端URL读取图片。
url = 'https://raw.githubusercontent.com/jmcnamara/XlsxWriter/' + \
'master/examples/logo.png'
image_data = BytesIO(urlopen(url).read())
# 向单元格写入字节流图片。注意,文件名必须显式指定。在这个例子中文件名会从URL字符串读取。
worksheet.insert_image('B2', url, {'image_data': image_data})
# 读取本地图片文件至字节流。注意,insert_image()方法可以直接这么干,此例只是为了演示。
filename = 'python.png'
image_file = open(filename, 'rb')
image_data = BytesIO(image_file.read())
image_file.close()
# 将字节流图片写入单元格,文件名必须显式指定。
worksheet.insert_image('B8', filename, {'image_data': image_data})
workbook.close()