python使用xlsxwriter
Posted 洛洛沙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python使用xlsxwriter相关的知识,希望对你有一定的参考价值。
1.导入:
import xlsxwriter
2.创建工作簿:
wb = xlsxwriter.Workbook(‘new.xlsx’)
3.添加工作表:
ws = wb.add_worksheet(‘图标‘)
4.调整列宽,或设置列的其他属性:
1 set_column(self, first_col, last_col, width=None, cell_format=None, 2 options=None): 3 """ 4 Set the width, and other properties of a single column or a 5 range of columns. 6 7 Args: 8 first_col: First column (zero-indexed). 9 last_col: Last column (zero-indexed). Can be same as first_col. 10 width: Column width. (optional). 11 cell_format: Column cell_format. (optional). 12 options: Dict of options such as hidden and level. 13 14 Returns: 15 0: Success. 16 -1: Column number is out of worksheet bounds. 17 18 """
5.调整行高,或设置行的其他属性:
1 set_row(self, row, height=None, cell_format=None, options=None): 2 """ 3 Set the width, and other properties of a row. 4 5 Args: 6 row: Row number (zero-indexed). 7 height: Row width. (optional). 8 cell_format: Row cell_format. (optional). 9 options: Dict of options such as hidden, level and collapsed. 10 11 Returns: 12 0: Success. 13 -1: Row number is out of worksheet bounds. 14 15 """
6.向单元格写入数据:
1 worksheet.write(0, 0, ‘Hello‘) # write_string() 2 worksheet.write(1, 0, ‘World‘) # write_string() 3 worksheet.write(2, 0, 2) # write_number() 4 worksheet.write(3, 0, 3.00001) # write_number() 5 worksheet.write(4, 0, ‘=SIN(PI()/4)‘) # write_formula() 6 worksheet.write(5, 0, ‘‘) # write_blank() 7 worksheet.write(6, 0, None) # write_blank()
7.插入图片:
1 insert_image(self, row, col, filename, options=None): 2 """ 3 Insert an image with its top-left corner in a worksheet cell. 4 5 Args: 6 row: The cell row (zero indexed). 7 col: The cell column (zero indexed). 8 filename: Path and filename for image in PNG, JPG or BMP format. 9 options: Position, scale, url and data stream of the image. 10 11 Returns: 12 0: Success. 13 -1: Row or column is out of worksheet bounds. 14 15 """
x_offset = options.get(‘x_offset‘, 0)
y_offset = options.get(‘y_offset‘, 0)
x_scale = options.get(‘x_scale‘, 1)
y_scale = options.get(‘y_scale‘, 1)
url = options.get(‘url‘, None)
tip = options.get(‘tip‘, None)
anchor = options.get(‘object_position‘, 2)
image_data = options.get(‘image_data‘, None)
options包含:x_offset、y_offset、x_scale、y_scale、url、tip、object_position、image_data
例子:
ws.insert_image(row, col, ‘D:\baidu.png‘, {‘url‘:‘www.baidu.com‘, ‘tip‘:‘百度‘, ‘x_scale‘:0.24, ‘y_scale‘:0.24})
以上是关于python使用xlsxwriter的主要内容,如果未能解决你的问题,请参考以下文章
使用 python-xlsxwriter 或 bash 打开受密码保护的 ods 文件