美文网首页
openpyxl3.0官方文档(21)—— 添加图案

openpyxl3.0官方文档(21)—— 添加图案

作者: Sinchard | 来源:发表于2020-07-10 13:52 被阅读0次

整个数据系列和单个数据点可以通过graphicalProperties属性设置样式。

    from openpyxl import Workbook
    from openpyxl.chart import BarChart, Reference
    from openpyxl.chart.marker import DataPoint
    
    from openpyxl.drawing.fill import PatternFillProperties, ColorChoice
    
    wb = Workbook()
    ws = wb.active
    
    rows = [
        ("Sample",),
        (1,),
        (2,),
        (3,),
        (2,),
        (3,),
        (3,),
        (1,),
        (2,),
    ]
    
    for r in rows:
        ws.append(r)
    
    
    c = BarChart()
    data = Reference(ws, min_col=1, min_row=1, max_row=8)
    c.add_data(data, titles_from_data=True)
    c.title = "Chart with patterns"
    
    # set a pattern for the whole series
    series = c.series[0]
    fill =  PatternFillProperties(prst="pct5")
    fill.foreground = ColorChoice(prstClr="red")
    fill.background = ColorChoice(prstClr="blue")
    series.graphicalProperties.pattFill = fill
    
    # set a pattern for a 6th data point (0-indexed)
    pt = DataPoint(idx=5)
    pt.graphicalProperties.pattFill = PatternFillProperties(prst="ltHorz")
    series.dPt.append(pt)
    
    ws.add_chart(c, "C1")
    
    wb.save("pattern.xlsx")
    
在这里插入图片描述

相关文章

网友评论

      本文标题:openpyxl3.0官方文档(21)—— 添加图案

      本文链接:https://www.haomeiwen.com/subject/rtqmcktx.html