How to fill faces with solid colors in a chart?
Code:
#Set Fill Properties 1 - Solid Color
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart.series import DataPoint
from openpyxl.chart.shapes import GraphicalProperties
from openpyxl.drawing.fill import (
GradientFillProperties,
GradientStop,
LinearShadeProperties
)
from openpyxl.drawing.colors import SchemeColor
from openpyxl.drawing.geometry import PresetGeometry2D
from openpyxl.drawing.line import LineProperties
# Create a new workbook and worksheet
wb=Workbook()
ws=wb.active
# Create a chart object
chart=BarChart()
# Set chart data
data=[
['Category', 'Value'],
['A', 10],
['B', 15],
['C', 7],
['D', 20]
]
#Add data to the worksheet
for row in data:
ws.append(row)
# Define chart data range
data_range=Reference(ws, min_col=2, min_row=1, max_col=2, max_row=5)
# Add data series to the chart
series=chart.add_data(data_range, titles_from_data=True)
# Set solid color for bar faces
chart.series[0].spPr=GraphicalProperties(
ln=LineProperties(solidFill='000000'),
#solidFill='00FF00'
noFill=True
)
#Disable gridlines
chart.y_axis.majorGridlines=None
# Insert the chart into the worksheet
ws.add_chart(chart, 'E3')
# Save the workbook
wb.save('test.xlsx')
