zoukankan      html  css  js  c++  java
  • Python 利用Python操作excel表格之openyxl介绍Part2

     

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

    by:授客 QQ1033553122

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

    ## 绘图
    c = LineChart()               设置图标类型:LineChart 连线图  AreaChart 面积图
    c.title = 'CPU利用率'        设置生成图的报告名称
    c.style = 10                  设置图例样式

    c.y_axis.title = '百分比'    设置 轴名称
    c.x_axis.title = '时间'      设置 轴名称

    c.y_axis.scaling.min = 0      设置y轴坐标最的小值
    c.y_axis.majorUnit = 10       设置主y轴坐标,两个坐标刻度直接的间隔
    c.y_axis.scaling.max = 100    设置主y轴坐标的最大值


    设置 data引用数据源:第2列到第列(包括第210),第1行到第30行,包括第1, 30
    data = Reference(sheet, min_col=2max_col=10min_row=1max_row=30)
    c.add_data(data, titles_from_data=True)

    设置x轴 坐标值,即轴标签(Label)(从第3列,第2(包括第2)开始取数据直到第30(包括30)
    x_labels = Reference(sheet, min_col=1min_row=2max_row=30)
    c.set_categories(x_labels)

    c.width = 18  设置图表的宽度 单位 cm
    c.height = 设置图表的高度 单位 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 + 1min_row=2max_row=300)
        series_obj = Series(ref_obj, title=target_column)
        c1.append(series_obj)


    x_labels = Reference(sheet, min_col=1min_row=2max_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=8min_row=2max_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


    #  新建工作簿
    # http://openpyxl.readthedocs.io/en/stable/tutorial.html#create-a-workbook
    work_book = Workbook()

    注:新建工作簿时会自动创建一个Sheet工作表,可通过如下方式获取默认新建的Sheet表,
    # work_book.active总是获取索引为0Sheet
    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'234567],
        ['Humans'104050201050],
    ]

    新增数据
    for row in rows:
        sheet4.append(row)

    work_book.save("secondary.xlsx")

     

    结果:

     

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

    控制台输出:

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

  • 相关阅读:
    SqlServer该如何合适的索引
    SqlServer该如何合适的索引
    SQLServer跨库查询分布式查询
    SQLServer跨库查询分布式查询
    sqlserver 字符串能不能执行
    WPF添加类库并引用
    StringUtils类常用方法
    如何理解.NET开发中的x86/x64/AnyCPU
    JS获取url参数
    MySQL CONCAT函数:字符串拼接
  • 原文地址:https://www.cnblogs.com/shouke/p/10157498.html
Copyright © 2011-2022 走看看