Patch for writing styles and graphs
Created originally on Bitbucket by Anonymous
Hi,
The attached patch allows for quite a number of things:
- writing cells styles
- writing graphs
- adding error bars
- defining legends labels
- writing shapes on graphs
- defining shapes colors, widths ...
- defining axis min/max
- defining graph drawing size, plot area size
- correct a garbage error when there is a style on cell but no content
The API support only bargraphs & linegraphs ATM, but should be easy to improve.
Example use:
{{{ #!python
import random
from openpyxl.workbook import Workbook from openpyxl.writer.excel import save_workbook from openpyxl.drawing import Shape from openpyxl.style import Color from openpyxl.chart import LineChart, BarChart, Serie, ErrorBar, Reference
wb = Workbook() ws = wb.get_active_sheet() ws.title = u'data'
define some data
for i in range(1,5): ws.cell(row=i, column=1).value = chr(65+i) for i in range(1,5): ws.cell(row=i, column=2).value = i for i in range(5,9): ws.cell(row=i-4, column=3).value = i for i in range(4): ws.cell(row=i+1, column=4).value = random.random()
define some series of data
serie1 = Serie(Reference(ws, (1,2), (4,2)), labels=Reference(ws, (1,1), (4,1)), legend=Reference(ws, (1,1)))
serie1.color = Color.DARKGREEN serie1.error_bar = ErrorBar(ErrorBar.PLUS_MINUS, Reference(ws, (1,4), (4,4)))
serie2 = Serie(Reference(ws, (1,3), (4,3)), labels=Reference(ws, (1,1), (4,1)))
chart = LineChart() # or BarChart chart.add_serie(serie1) chart.add_serie(serie2)
chart container dimensions in pixels
chart.drawing.left = 10 chart.drawing.top = 20 chart.drawing.height = 200 chart.drawing.width = 500
chart area in percentage of the container
chart.width = .7 chart.height = .7 chart.margin_top = .2
add some shapes
shapes are positionned in data units
s = Shape(((0,0), (2,-0.5))) chart.add_shape(s)
s = Shape(((3,-.5), (4,1.5))) s.border_color = Color.RED s.border_width = 2 s.text = u'lolo' chart.add_shape(s)
ws.add_chart(chart)
yeah, I'm on windows... :-(
wb.save(r'c:\temp\toto.xlsx')
}}}
Attachments: openpyxl.patch