OpenPyXL 3.0.7
  • 教程
  • 简单用法
  • 性能
  • 优化模式
  • 插入和删除行和列,移动单元格范围
  • 操纵 Pandas 和 NumPy
  • 图表
  • 注释
  • 操控样式
  • 额外工作表特性
  • 条件格式化
  • 数据透视表
  • 打印设置
  • 使用过滤器和排序
  • 验证单元格
  • 定义名称
  • 工作表表格
  • 剖析公式
  • 日期和时间
  • 保护
  • 开发
  • openpyxl 包
  • 3.0.7 (2021-03-09)
  • 3.0.6 (2021-01-14)
  • 3.0.5 (2020-08-21)
  • 3.0.4 (2020-06-24)
  • 3.0.3 (2020-01-20)
  • 3.0.2 (2019-11-25)
  • 3.0.1 (2019-11-14)
  • 3.0.0 (2019-09-25)
  • 2.6.4 (2019-09-25)
  • 2.6.3 (2019-08-19)
  • 2.6.2 (2019-03-29)
  • 2.6.1 (2019-03-04)
  • 2.6.0 (2019-02-06)
  • 2.6.-b1 (2019-01-08)
  • 2.6-a1 (2018-11-21)
  • 2.5.14 (2019-01-23)
  • 2.5.13 (brown bag)
  • 2.5.12 (2018-11-29)
  • 2.5.11 (2018-11-21)
  • 2.5.10 (2018-11-13)
  • 2.5.9 (2018-10-19)
  • 2.5.8 (2018-09-25)
  • 2.5.7 (2018-09-13)
  • 2.5.6 (2018-08-30)
  • 2.5.5 (2018-08-04)
  • 2.5.4 (2018-06-07)
  • 2.5.3 (2018-04-18)
  • 2.5.2 (2018-04-06)
  • 2.5.1 (2018-03-12)
  • 2.5.0 (2018-01-24)
  • 2.5.0-b2 (2018-01-19)
  • 2.5.0-b1 (2017-10-19)
  • 2.5.0-a3 (2017-08-14)
  • 2.5.0-a2 (2017-06-25)
  • 2.5.0-a1 (2017-05-30)
  • 2.4.11 (2018-01-24)
  • 2.4.10 (2018-01-19)
  • 2.4.9 (2017-10-19)
  • 2.4.8 (2017-05-30)
  • 2.4.7 (2017-04-24)
  • 2.4.6 (2017-04-14)
  • 2.4.5 (2017-03-07)
  • 2.4.4 (2017-02-23)
  • 2.4.3 (未发行)
  • 2.4.2 (2017-01-31)
  • 2.4.1 (2016-11-23)
  • 2.4.0 (2016-09-15)
  • 2.4.0-b1 (2016-06-08)
  • 2.4.0-a1 (2016-04-11)
  • 2.3.5 (2016-04-11)
  • 2.3.4 (2016-03-16)
  • 2.3.3 (2016-01-18)
  • 2.3.2 (2015-12-07)
  • 2.3.1 (2015-11-20)
  • 2.3.0 (2015-10-20)
  • 2.3.0-b2 (2015-09-04)
  • 2.3.0-b1 (2015-06-29)
  • 2.2.6 (未发行)
  • 2.2.5 (2015-06-29)
  • 2.2.4 (2015-06-17)
  • 2.2.3 (2015-05-26)
  • 2.2.2 (2015-04-28)
  • 2.2.1 (2015-03-31)
  • 2.2.0 (2015-03-11)
  • 2.2.0-b1 (2015-02-18)
  • 2.1.5 (2015-02-18)
  • 2.1.4 (2014-12-16)
  • 2.1.3 (2014-12-09)
  • 2.1.2 (2014-10-23)
  • 2.1.1 (2014-10-08)
  • 2.1.0 (2014-09-21)
  • 2.0.5 (2014-08-08)
  • 2.0.4 (2014-06-25)
  • 2.0.3 (2014-05-22)
  • 2.0.2 (2014-05-13)
  • 2.0.1 (2014-05-13) brown bag
  • 2.0.0 (2014-05-13) brown bag
  • 1.8.6 (2014-05-05)
  • 1.8.5 (2014-03-25)
  • 1.8.4 (2014-02-25)
  • 1.8.3 (2014-02-09)
  • 1.8.2 (2014-01-17)
  • 1.8.1 (2014-01-14)
  • 1.8.0 (2014-01-08)
  • 1.7.0 (2013-10-31)
  • Axis Limits and Scale

    Minima and Maxima

    Axis minimum and maximum values can be set manually to display specific regions on a chart.

    from openpyxl import Workbook
    from openpyxl.chart import (
        ScatterChart,
        Reference,
        Series,
    )
    wb = Workbook()
    ws = wb.active
    ws.append(['X', '1/X'])
    for x in range(-10, 11):
        if x:
            ws.append([x, 1.0 / x])
    chart1 = ScatterChart()
    chart1.title = "Full Axes"
    chart1.x_axis.title = 'x'
    chart1.y_axis.title = '1/x'
    chart1.legend = None
    chart2 = ScatterChart()
    chart2.title = "Clipped Axes"
    chart2.x_axis.title = 'x'
    chart2.y_axis.title = '1/x'
    chart2.legend = None
    chart2.x_axis.scaling.min = 0
    chart2.y_axis.scaling.min = 0
    chart2.x_axis.scaling.max = 11
    chart2.y_axis.scaling.max = 1.5
    x = Reference(ws, min_col=1, min_row=2, max_row=22)
    y = Reference(ws, min_col=2, min_row=2, max_row=22)
    s = Series(y, xvalues=x)
    chart1.append(s)
    chart2.append(s)
    ws.add_chart(chart1, "C1")
    ws.add_chart(chart2, "C15")
    wb.save("minmax.xlsx")
    					
    "Sample charts with examples of axis clipping"

    注意

    In some cases such as the one shown, setting the axis limits is effectively equivalent to displaying a sub-range of the data. For large datasets, rendering of scatter plots (and possibly others) will be much faster when using subsets of the data rather than axis limits in both Excel and Open/Libre Office.

    Logarithmic Scaling

    Both the x- and y-axes can be scaled logarithmically. The base of the logarithm can be set to any valid float. If the x-axis is scaled logarithmically, negative values in the domain will be discarded.

    from openpyxl import Workbook
    from openpyxl.chart import (
        ScatterChart,
        Reference,
        Series,
    )
    import math
    wb = Workbook()
    ws = wb.active
    ws.append(['X', 'Gaussian'])
    for i, x in enumerate(range(-10, 11)):
        ws.append([x, "=EXP(-(($A${row}/6)^2))".format(row = i + 2)])
    chart1 = ScatterChart()
    chart1.title = "No Scaling"
    chart1.x_axis.title = 'x'
    chart1.y_axis.title = 'y'
    chart1.legend = None
    chart2 = ScatterChart()
    chart2.title = "X Log Scale"
    chart2.x_axis.title = 'x (log10)'
    chart2.y_axis.title = 'y'
    chart2.legend = None
    chart2.x_axis.scaling.logBase = 10
    chart3 = ScatterChart()
    chart3.title = "Y Log Scale"
    chart3.x_axis.title = 'x'
    chart3.y_axis.title = 'y (log10)'
    chart3.legend = None
    chart3.y_axis.scaling.logBase = 10
    chart4 = ScatterChart()
    chart4.title = "Both Log Scale"
    chart4.x_axis.title = 'x (log10)'
    chart4.y_axis.title = 'y (log10)'
    chart4.legend = None
    chart4.x_axis.scaling.logBase = 10
    chart4.y_axis.scaling.logBase = 10
    chart5 = ScatterChart()
    chart5.title = "Log Scale Base e"
    chart5.x_axis.title = 'x (ln)'
    chart5.y_axis.title = 'y (ln)'
    chart5.legend = None
    chart5.x_axis.scaling.logBase = math.e
    chart5.y_axis.scaling.logBase = math.e
    x = Reference(ws, min_col=1, min_row=2, max_row=22)
    y = Reference(ws, min_col=2, min_row=2, max_row=22)
    s = Series(y, xvalues=x)
    chart1.append(s)
    chart2.append(s)
    chart3.append(s)
    chart4.append(s)
    chart5.append(s)
    ws.add_chart(chart1, "C1")
    ws.add_chart(chart2, "I1")
    ws.add_chart(chart3, "C15")
    ws.add_chart(chart4, "I15")
    ws.add_chart(chart5, "F30")
    wb.save("log.xlsx")
    					

    This produces five charts that look something like this:

    "Sample charts with examples of axis log scaling"

    The first four charts show the same data unscaled, scaled logarithmically in each axis and in both axes, with the logarithm base set to 10. The final chart shows the same data with both axes scaled, but the base of the logarithm set to e .

    Axis Orientation

    Axes can be displayed “normally” or in reverse. Axis orientation is controlled by the scaling orientation property, which can have a value of either 'minMax' for normal orientation or 'maxMin' for reversed.

    from openpyxl import Workbook
    from openpyxl.chart import (
        ScatterChart,
        Reference,
        Series,
    )
    wb = Workbook()
    ws = wb.active
    ws["A1"] = "Archimedean Spiral"
    ws.append(["T", "X", "Y"])
    for i, t in enumerate(range(100)):
        ws.append([t / 16.0, "=$A${row}*COS($A${row})".format(row = i + 3),
                             "=$A${row}*SIN($A${row})".format(row = i + 3)])
    chart1 = ScatterChart()
    chart1.title = "Default Orientation"
    chart1.x_axis.title = 'x'
    chart1.y_axis.title = 'y'
    chart1.legend = None
    chart2 = ScatterChart()
    chart2.title = "Flip X"
    chart2.x_axis.title = 'x'
    chart2.y_axis.title = 'y'
    chart2.legend = None
    chart2.x_axis.scaling.orientation = "maxMin"
    chart2.y_axis.scaling.orientation = "minMax"
    chart3 = ScatterChart()
    chart3.title = "Flip Y"
    chart3.x_axis.title = 'x'
    chart3.y_axis.title = 'y'
    chart3.legend = None
    chart3.x_axis.scaling.orientation = "minMax"
    chart3.y_axis.scaling.orientation = "maxMin"
    chart4 = ScatterChart()
    chart4.title = "Flip Both"
    chart4.x_axis.title = 'x'
    chart4.y_axis.title = 'y'
    chart4.legend = None
    chart4.x_axis.scaling.orientation = "maxMin"
    chart4.y_axis.scaling.orientation = "maxMin"
    x = Reference(ws, min_col=2, min_row=2, max_row=102)
    y = Reference(ws, min_col=3, min_row=2, max_row=102)
    s = Series(y, xvalues=x)
    chart1.append(s)
    chart2.append(s)
    chart3.append(s)
    chart4.append(s)
    ws.add_chart(chart1, "D1")
    ws.add_chart(chart2, "J1")
    ws.add_chart(chart3, "D15")
    ws.add_chart(chart4, "J15")
    wb.save("orientation.xlsx")
    					

    This produces four charts with the axes in each possible combination of orientations that look something like this:

    "Sample charts with different axis orientations"