前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 利用Python操作excel表格之openyxl介绍Part2

Python 利用Python操作excel表格之openyxl介绍Part2

作者头像
授客
发布2019-09-11 16:46:11
9210
发布2019-09-11 16:46:11
举报
文章被收录于专栏:授客的专栏授客的专栏

欢迎加入全国软件测试交流qq群(群号:7156436)

## 绘图 c = LineChart() # 设置图标类型:LineChart 连线图 AreaChart 面积图 c.title = 'CPU利用率' # 设置生成图的报告名称 c.style = 10 # 设置图例样式 c.y_axis.title = '百分比' # 设置 Y 轴名称 c.x_axis.title = '时间' # 设置 X 轴名称 c.y_axis.scaling.min = 0 # 设置y轴坐标最的小值 c.y_axis.majorUnit = 10 # 设置主y轴坐标,两个“坐标刻度”直接的间隔 c.y_axis.scaling.max = 100 # 设置主y轴坐标的最大值 # 设置 data引用数据源:第2列到第列(包括第2,10列),第1行到第30行,包括第1, 30行 data = Reference(sheet, min_col=2, max_col=10, min_row=1, max_row=30) c.add_data(data, titles_from_data=True) # 设置x轴 坐标值,即轴标签(Label)(从第3列,第2行(包括第2行)开始取数据直到第30行(包括30行)) x_labels = Reference(sheet, min_col=1, min_row=2, max_row=30) c.set_categories(x_labels) c.width = 18 # 设置图表的宽度 单位 cm c.height = 8 # 设置图表的高度 单位 cm # 设置插入图表位置 cell = "A10" sheet.add_chart(c, cell) # 绘制双y坐标轴图表 sheet = work_book['DEV'] c1 = AreaChart() # 面积图 c1.title = '磁盘活动统计报告' c1.style = 10 # 10 13 11 c1.y_axis.title = '平均时长(毫秒)' c1.x_axis.title = '时间' c1.y_axis.majorGridlines = None first_row = [] # 存储第一行记录 # 获取第一行记录 for row in sheet.rows: for cell in row: first_row.append(cell.value) break # 拼接系列的方式 target_columns = ['await', 'svctm'] for target_column in target_columns: index = first_row.index(target_column) ref_obj = Reference(sheet, min_col=index + 1, min_row=2, max_row=300) series_obj = Series(ref_obj, title=target_column) c1.append(series_obj) x_labels = Reference(sheet, min_col=1, min_row=2, max_row=300) c1.set_categories(x_labels) c1.width = 18 c1.height = 8 c2 = LineChart() c2.y_axis.title = '磁盘利用率' c2.y_axis.scaling.min = 0 # 设置y轴坐标最的小值 #c2.y_axis.majorUnit = 5 # 设置主y轴坐标的坐标单位 c2.y_axis.scaling.max = 100 # 设置主y轴坐标的最大值 ref_obj = Reference(sheet, min_col=8, min_row=2, max_row=300) series_obj = Series(ref_obj, title='%util') c2.append(series_obj) s = c2.series[0] # 获取添加第一个系列 # 设置线条填充颜色,也是图例的颜色 s.graphicalProperties.line.solidFill = "DEB887" # 设置线形 可选值如下: # ['solid', 'dot', 'dash', 'lgDash', 'dashDot', 'lgDashDot', 'lgDashDotDot', 'sysDash', 'sysDot', 'sysDashDot','sysDashDotDot'] s.graphicalProperties.line.dashStyle = "sysDot" s.graphicalProperties.line.width = 50000 # 设置线条宽度(单位:EMUs s.smooth = True # 设置平滑线条 # 设置第二个图表的y轴同x轴的交叉点为最大值 max,以便让其y轴靠图表最右侧展示 c2.y_axis.crosses = "max" # 可选值:autoZero、 min、 max c2.y_axis.axId = 200 # 我也不知道做啥用,反正不能少,值固定200就可以了 c1 += c2 sheet.add_chart(c1, "A2") work_book.save('./new_mydata.xlsx') # 保存、另存为工作簿

结果:

Python <wbr>利用Python操作excel表格之openyxl介绍Part2
Python <wbr>利用Python操作excel表格之openyxl介绍Part2

# 新建工作簿 # http://openpyxl.readthedocs.io/en/stable/tutorial.html#create-a-workbook work_book = Workbook() # 注:新建工作簿时会自动创建一个Sheet工作表,可通过如下方式获取默认新建的Sheet表, # work_book.active总是获取索引为0的Sheet表 sheet = work_book.active # 插入一个新的Sheet表到最前面 mysheet1 = work_book.create_sheet(title='MySheet1', index=0) # 在最后面增加一个Sheet表 mysheet2 = work_book.create_sheet(title='MySheet2') # 修改Sheet工作表的名称 mysheet2.title = 'Sheet3' # 修改sheet标签颜色 mysheet2.sheet_properties.tabColor = "1072BA" sheet.row_dimensions[1].height = 7 # 设置行高 单位 cm( 第一行行高7cm sheet.column_dimensions['A'].width = 14 # 设置列宽 单位 cm( A列列宽设置为14cm # 复制Sheet工作表 sheet4 = work_book.copy_worksheet(mysheet2) rows = [ ['Aliens', 2, 3, 4, 5, 6, 7], ['Humans', 10, 40, 50, 20, 10, 50], ] # 新增数据 for row in rows: sheet4.append(row) work_book.save("secondary.xlsx")

结果:

Python <wbr>利用Python操作excel表格之openyxl介绍Part2
Python <wbr>利用Python操作excel表格之openyxl介绍Part2

控制台输出:

Python <wbr>利用Python操作excel表格之openyxl介绍Part2
Python <wbr>利用Python操作excel表格之openyxl介绍Part2
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档