可以使用Openpyxl创建具有动态更新高度的ErrorBars吗?

时间:2016-08-30 01:26:48

标签: python excel charts openpyxl

A previous question展示了如何将错误栏高度设置为在python中设置的固定值。是否可以设置错误栏高度为excel在其他单元格中存储的计算值?理想情况下,更改基础数据可能会改变错误栏高度?

就此而言,excel甚至可以做到吗?

这是我的尝试:

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart.data_source import NumDataSource, NumData, NumVal, NumRef
from openpyxl.chart.series import ErrorBars
from openpyxl.chart.series_factory import SeriesFactory
from random import randint

# Create a new workbook
book = Workbook(encoding="utf-8")
sheet = book.active

# Write 5 random ints to worksheet
for i in range (1,6):
    sheet.cell(row=i,column=1,value=randint(1,100))


# write formula to computer Average  
sheet.cell(row=6,column=1,value='=AVERAGE(A1:A5)')                

# write formula to compute StdDev
sheet.cell(row=7,column=1,value='=_xlfn.STDEV.S(A1:A4)/10')

# create a chart with single bar whose height is the Average As Calculated by Excel
chart = BarChart()
barheight = Reference(sheet, min_col=1, min_row=6, max_col=1, max_row=6)
series = SeriesFactory(barheight)

# This creates an error bar of static height 5  
myNumVals = [NumVal(0, None, v=5)]    
myNumData = NumData(pt=myNumVals)
myNumDataSource = NumDataSource(numLit=myNumData)

# This seems like something that might work, but it doesn't work.  
# myNumDataSource = NumDataSource(NumRef("A7"))
myErrorBars=ErrorBars(plus=myNumDataSource,minus=myNumDataSource,errDir='y',errValType='cust')
series.errBars = myErrorBars

chart.series.append(series)
sheet.add_chart(chart, "A9")
book.save('output.xlsx')

0 个答案:

没有答案