Is there a way to create a chart in XlsxWriter without writing the data to cells in a sheet? IE. From an array in the python script?

Question:

My goal is to create a chart in a sheet from XlsxWriter without having the data from the chart available anywhere on the sheet (I don’t want to reference cells). For legal reasons the data cannot be available in the book.

Alternatively, if there’s a way to write data to cells, graph that data, and then remove the cell references so I can have a graph that isn’t referencing cells, I would be open to that as well. I know there are some ways to accomplish this in excel.

I have tried replacing the ‘categories’ and ‘values’ arguments with lists of integers (data I want graphed) but I get an error for a lack of cell references. (See function below).

# Builds a chart with top left corner at row_start, col_start (zero 
# indexed).

# data[data_input[i][0]]['history'][0] is a list of dates.
# data[data_input[i][0]]['history'][1] is a list of values for those dates.

chart = workbook.add_chart({'type': 'line'})

for i in range(len(data_input)):
    chart.add_series({
        'name':       title,
        'categories': data[data_input[i][0]]['history'][0],
        'values':     data[data_input[i][0]]['history'][1],
        'line':       {'color': colors[i], 'width': 1}
    })

worksheet.insert_chart(row_start, col_start, chart)

TypeError: xl_range_formula() takes 5 positional arguments but 261 were given

Asked By: johnyt3

||

Answers:

My goal is to create a chart in a sheet from XlsxWriter without having the data from the chart available anywhere on the sheet. For legal reasons the data cannot be available in the book.

That isn’t possible.

In Excel it is possible to draw a graph from data that you supply directly in the data dialog without referring to cells. However, that option isn’t supported by XlsxWriter and it also doesn’t hide your data in any way since anyone can go into the dialog and see the data. Also, anyone could unzip the file and see the data.

Also, anyone could mouse over the data in the chart and see the values that it is generated from.

Alternatively, if there’s a way to write data to cells, graph that data, and then remove the cell references so I can have a graph that isn’t referencing cells,

No, for the same reasons as above.

Take a step back and try to figure out if what you want to do is possible in any way just in Excel (possibly by hiding a worksheet with the source data and then locking and password protecting the file). If there is a way to do it in Excel then you know the question to ask about how to do it in Python.

As a suggestion, one way to do it could be to create an image of a chart and insert that into Excel. That way there is no link to the original data.

Answered By: jmcnamara

One option is to save the chart as an image and insert the image to your excel worksheet using xlsxwriter’s insert_image

plot = your_df.plot()
fig = plot.get_figure()
fig.savefig("output.png")




# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('images.xlsx')
worksheet = workbook.add_worksheet()


worksheet.insert_image('B2', 'output.png')

saving the plot

Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig

inserting the image

https://xlsxwriter.readthedocs.io/example_images.html

Answered By: ZakS
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.