python xlsxwriter使用方法汇总

xlsxwriter用来写入excel的模块

1、创建一个excel文件

import xlsxwriter
filename = '/Users/piperck/Desktop/demo.xlsx'
test_book = xlsxwriter.Workbook(filename)

  

2、添加一个sheet页,向sheet页中单元格中写入数据

work_sheet = test_book.add_worksheet()
work_sheet.write_number("A1",12)
# work_sheet.write_number(0,0,12) # (第几排,第几列,所添加的数字)
work_sheet.write_number("A2",13)
work_sheet.write_formula("A3", "=SUM(A1:A2)")
# work_sheet.write_formula(2,0, "=SUM(A1:A2)"

注:定位单元格,可以用字母数字组合(A1),也可以用坐标形式(0,0)

向单元格中写入可以分为以下方法:

  • writer_number() 向单元格中写入数字
  • write_blank()将一个空白写入单元格
  • write_string()将字符串写入单元格
  • write_formula() 填入公式
  • write_array_formula()写入公式
  • write_datetime()填写日期 # 必须填入date,time 对象
  • write_boolean()填入Boolean值
  • write_url()填入url
  • write_rich_string(row, col, *args)填写多种格式的字符串
  • write()调用适当的write方法

给单元格设置样式

  • add_format() 
wbk = xlsxwriter.Workbook('test.xlsx')
sheet = wbk.add_worksheet()
title_style = wbk.add_format({
                "bold": True,
                'font_name': '仿宋',
                'font_size': 14,
                "align": 'center',
                "valign": 'vcenter',
                'text_wrap': 1
            }
        )
        
sheet.merge_range("A1:P1", 'test for style', title_style)

其他方法

     其中需要传入row,col 参数的是用来确定从哪个单元格开始写入,可以将参数替换为字母与数字的组合如‘A1’对应(row=0,col=0)

  • write_row(row, col, data, cell_format=None)

    • 从行(col)开始写,列固定
  • write_column(row, col, data, cell_format=None)

    • 从列(row)开始写,行固定
  • insert_image(row, col, filename, options=None)

    • 插入图片
      • filename:PNG、JPG或BMP格式的图像的路径和文件名。
  • insert_textbox(row, col, text, options=None)

    • 插入文本框
  • insert_chart(row, col, chart, options=None)

    • 插入图表
      • options 图表的位置和规模
  • write_comment(row, col, comment, options=None)

    • 为单元格添加注释
  • show_comments

    • 展示单元格注释
  • set_comments_author(author)

    • 设置评论作者
  • get_name()

    • 获取工作表的名称
  • activate()

    • 将工作表设置为活跃的,打开excel 第一个显示的表
  • select()

    • 将工作表设置为选中工作表,高亮显示
  • hide()

    • 隐藏选中的工作表
  • set_column(firstcol, lastcol,width=None,cell_format=None, options=None)

    • 设置列的宽度
  • set_first_sheet() -将当前工作表设置为第一个可见的表

  • set_row(row, height=None, cell_format=None, options=None)

    • 设置行的宽度及其他属性值
  • set_default_row(self, height=None, hide_unused_rows=False)

    • 设置默认行属性
      • hide_unused_rows: 隐藏未使用的行
  • merge_range(first_row, first_col, last_row,last_col,data, cell_format=None)

    • 合并单元格
  • autofilter(first_row, first_col, last_row, last_col)

    • 在工作表中设置自动过滤区域
  • filter_column(self, col, criteria)

    • 设置筛选滤条件
      • criteria: 筛选条件
  • filter_column_list(self, col, filters)

    • 在excel2007 列表样式中设置筛选标准
  • data_validation(first_row, first_col, last_row, last_col,options)

    • 添加数据验证
  • conditional_format(first_row, first_col, last_row, last_col,options=None)

    • 向工作表中添加一个条件格式
  • add_table(self, first_row, first_col, last_row, last_col,options=None)

    • 向excel中添加一个工作表
  • add_sparkline(self, row, col, options)

    • 添加微线图
  • set_selection(first_row, first_col, last_row, last_col)

    • 设置选定的单元格
  • dset_zoom(zoom=100)

    • 设置缩放 10-400
  • freeze_panes( row, col, top_row=None, left_col=None, pane_type=0)

    • 创建工作表窗格,并设置为冻结
  • split_panes(x, y, top_row=None, left_col=None)

    • 创建工作表窗格,并将其标记为分割
      • x:垂直分割的位置。
      • y:水平分割的位置。
      • toprow:在窗格的滚动区域中最可见的行。
      • left_col:在窗格的滚动区域中最可见的行。
  • hide_zero()

    • 自工作表中隐藏0值
  • set_tab_color(color)

    • 设置 工作表选项卡的颜色
  • protect(password='', options=None)

    • 设置工作的密码和保护项
      • options: 用于保护的工作表对象的字典
  • insert_button(row, col, options=None)

    • 插入一个button表单对象
  • set_footer(footer='', options=None, margin=None)

    • 设置页脚标题和可选的页边距
  • set_header(header='',options=None,margin=None)

    • 设置页面标题标题和可选的页边距
  • set_margins(left=0.7, right=0.7, top=0.75, bottom=0.75)

    • 将所有的页边距设置为英寸
  • set_paper(self, paper_size)

    • 设置纸张类型 papaer_size: A4=9
  • set_portrait()

    • 将页面朝向设置为竖向
  • set_landscape()

    • 将页面的朝向设置为横向

猜你喜欢

转载自www.cnblogs.com/Frange/p/9204166.html