1、安装xlsxwriter模块
- 使用
pip install XlsxWriter 命令进行安装
2、使用xlsxwriter模块
import xlsxwriter
wb = xlsxwriter.Workbook("demo.xlsx")
ws = wb.add_worksheet("sheet-1")
ws.writer("A1", "hello, world")
wb.close()
3、常用用法
- 写入单个单元格用
write ,可以使用A1 或者0, 0 表示第1行第1列的单元格ws.writer("A1", "hello, A1", format)
ws.writer(0, 1, "hello, (0, 1)", format)
- 设置单元格的格式使用
add_format format = wb.add_format(
{
"bold": False,
"border": 1,
"align": "center",
"valign": "vcenter",
"fg_color": "#FFFF00"
}
)
ws.write("A1", "hello", format)
- 使用
write_row 写入行rows = ["第一轮bug", "第二轮bug", "第三轮bug", "第四轮bug"]
ws.write_row("A3", rows, format)
- 使用
write_column 写入列cols = ["1号人员", "2号人员", "3号人员", "4号人员"]
ws.write_column("A4", cols, format)
- 使用
merge_range 合并单元格ws.merge_range("B4:E7", "数据", format)
4、其他函数
def set_column(self, first_col, last_col, width=None, cell_format=None,
options=None):
"""
Set the width, and other properties of a single column or a
range of columns.
Args:
first_col: First column (zero-indexed).
last_col: Last column (zero-indexed). Can be same as first_col.
width: Column width. (optional).
cell_format: Column cell_format. (optional).
options: Dict of options such as hidden and level.
Returns:
0: Success.
-1: Column number is out of worksheet bounds.
"""
def set_row(self, row, height=None, cell_format=None, options=None):
"""
Set the width, and other properties of a row.
Args:
row: Row number (zero-indexed).
height: Row height. (optional).
cell_format: Row cell_format. (optional).
options: Dict of options such as hidden, level and collapsed.
Returns:
0: Success.
-1: Row number is out of worksheet bounds.
"""
def set_default_row(self, height=None, hide_unused_rows=False):
"""
Set the default row properties.
Args:
height: Default height. Optional, defaults to 15.
hide_unused_rows: Hide unused rows. Optional, defaults to False.
Returns:
Nothing.
"""
|