Python 柱状图写入表格
学Python,用RPA
艺赛旗RPA2020.1版本 正在免费下载使用中,欢迎下载使用
www.i-search.com.cn/index.html?from=line1
#!/usr/bin/env Python3
-- coding: utf-8 --
@Software: PyCharm
@virtualenv:workon
@contact: [email protected]
@Desc:Code descripton
author = ‘未昔/AngelFate’
date = ‘2020/4/28 12:49’
-- coding:utf-8 --
import xlsxwriter
创建一个excel
workbook = xlsxwriter.Workbook(“chart_column.xlsx”)
创建一个sheet
worksheet = workbook.add_worksheet()
worksheet = workbook.add_worksheet(“bug_analysis”)
自定义样式,加粗
bold = workbook.add_format({‘bold’: 1})
--------1、准备数据并写入excel---------------
向excel中写入数据,建立图标时要用到
headings = [‘Number’, ‘testA’, ‘testB’]
data = [
[‘2020-4-21’, ‘2020-4-22’, ‘2020-4-23’, ‘2020-4-24’, ‘2020-4-25’, ‘2020-4-26’],
[50, 40, 50, 60, 70, 50],
[30, 60, 60, 50, 40, 30],
]
写入表头
worksheet.write_row(‘A1’, headings, bold)
写入数据
worksheet.write_column(‘A2’, data[0])
worksheet.write_column(‘B2’, data[1])
worksheet.write_column(‘C2’, data[2])
--------2、生成图表并插入到excel---------------
创建一个柱状图(column chart)
chart_col = workbook.add_chart({‘type’: ‘column’})
配置第一个系列数据
chart_col.add_series({
# 这里的sheet1是默认的值,因为我们在新建sheet时没有指定sheet名
# 如果我们新建sheet时设置了sheet名,这里就要设置成相应的值
‘name’: ‘=Sheet1!$BAABB$7’,
‘line’: {‘color’: ‘red’},
})
配置第二个系列数据(用了另一种语法)
chart_col.add_series({
‘name’: ‘=Sheet1!$CAACC$7’,
‘line’: {‘color’: ‘yellow’},
})
配置第二个系列数据(用了另一种语法)
chart_col.add_series({
‘name’: [‘Sheet1’, 0, 2],
‘categories’: [‘Sheet1’, 1, 0, 6, 0],
‘values’: [‘Sheet1’, 1, 2, 6, 2],
‘line’: {‘color’: ‘yellow’},
})
设置图表的title 和 x,y轴信息
chart_col.set_title({‘name’: ‘The xxx site Bug Analysis’})
chart_col.set_x_axis({‘name’: ‘Test number’})
chart_col.set_y_axis({‘name’: ‘Sample length (mm)’})
设置图表的风格
chart_col.set_style(1)
把图表插入到worksheet以及偏移
worksheet.insert_chart(‘A10’, chart_col, {‘x_offset’: 25, ‘y_offset’: 10})
workbook.close()