openpyxl barchart LibreOffice compatibility issue

813 views Asked by At

I want to create an Excel file using pythons openpyxl like in this video: https://youtu.be/fqvZZp2q2uE

Code is easy:

# See full Toturial at my Youtube Channel(YB TV): https://www.youtube.com/channel/UCvnhhDKv5takEN412dmVW8g/featured
# GitHab Page:https://github.com/yasser64b/
#Email: [email protected]

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series, LineChart, ScatterChart
from openpyxl.styles import Font, Color, colors

wb = Workbook()
ws = wb.active
for i in range(10):
    ws.append([i])

# drawing a graph
values = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=10)
# chart = LineChart()
chart = BarChart()
ws.add_chart(chart, "A15")
chart.title = "Chart"
chart.y_axis.title = 'Size'
chart.x_axis.title = 'Test Number'
chart.add_data(values)

s1 = chart.series[0]
s1.marker.symbol = "triangle"



wb.save("Chart-1.xlsx")

But I have problems opening it with LibreOffice Calc:

LibreOffice Calc

If opening with Gnumeric it looks better:

Gnumeric

What can I do to make the .xlsx file more compatible with LibreOffice Calc?

3

There are 3 answers

0
masterofpuppets On BEST ANSWER

It's a bug in LibreOffice that will be fixed in version 7.1.0:

https://bugs.documentfoundation.org/show_bug.cgi?id=137734

1
Mikhail Ilin On

If you don't specifically need to use LibreOffice, but simply a free equivalent of excel, you can use OpenOffice. From my experience it works better with openpyxl.

0
Sita On

Probably not a very useful solution, as you need access to MS Excel, but for me it works to open the file in MS Excel first, save it from there, and then open it in LibreOffice (not sure whether this works with Gnumeric too; somehow my Gnumeric version refuses to open Openpyxl-generated charts).

This is a lot of hassle and, as said, useless if you don't have access to MS Excel, so if anyone is aware of a more direct solution, I'd love to hear it.