2017-08-07 216 views
-2

基于在openpyxl值(负或正值)如何更改图表的颜色?
真的很难为我。如何根据openpyxl中的值(负值或正值)更改图表颜色?

from openpyxl import Workbook 
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill,Alignment, Color, numbers 
from openpyxl.chart import BarChart, Reference 

book = Workbook() 
sheet = book.active 
rows = [ 
['Number', 'Batch 1'], 
['a', 40], 
['b', -40], 
['c', 50], 
['d', -30], 
['e', -25], 
['f', 30], 
] 

for row in rows: 
    sheet.append(row) 

chart = BarChart() 
chartCats = Reference(sheet, min_col=1, min_row=2, max_row=7) 
chartData = Reference(sheet, min_col=2, min_row=1, max_col=2, max_row=7) 
chart.add_data(chartData, titles_from_data = True) 
chart.set_categories(chartCats) 
chart.type = 'bar' 
chart.style = 27 
chart.height = 10 
chart.width = 15 
chart.legend = None 
chart.x_axis.tickLblPos = "low" 
chart.y_axis.number_format = '#,##0' 
chart.y_axis.majorGridlines = None 
sheet.add_chart(chart, 'A1') 
book.save('test.xlsx') 

上面的代码创建标题为“Batch1”的图表。但我想要“Batch2”,其中负值和正值显示不同的颜色。

+1

你能证明在*解决这个自己的任何努力*?甚至任何代码显示您正在尝试执行此操作的上下文? –

+0

从这个(正值和负值共享相同的颜色) ![图片1](https://i.stack.imgur.com/QRRmk.png) 对这一个(不同的颜色) ![图片2]( https://i.stack.imgur.com/QURbT.png) – JJJ

+1

这只是图片说明你的*问题*;你没有向*解决方案*展示任何努力,也没有你需要修改的代码的任何细节。 –

回答