简体   繁体   English

有没有办法在 XlsxWriter 中创建图表而不将数据写入工作表中的单元格? IE。 来自 python 脚本中的数组?

[英]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?

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).我的目标是在 XlsxWriter 的工作表中创建一个图表,而无需在工作表上的任何位置提供图表中的数据(我不想引用单元格)。 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.我知道在 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.我尝试用整数列表(我想要绘制的数据)替换“类别”和“值”arguments,但由于缺少单元格引用而出现错误。 (See function below). (参见下面的 function)。

# 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 TypeError: xl_range_formula() 需要 5 个位置 arguments 但给出了 261

My goal is to create a chart in a sheet from XlsxWriter without having the data from the chart available anywhere on the sheet.我的目标是在 XlsxWriter 的工作表中创建图表,而无需在工作表上的任何位置使用图表中的数据。 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.在 Excel 中,可以根据您直接在数据对话框中提供的数据绘制图形,而无需引用单元格。 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.但是,XlsxWriter 不支持该选项,并且它也不会以任何方式隐藏您的数据,因为任何人都可以进入对话框查看数据。 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).退后一步,尝试弄清楚您是否可以仅在 Excel 中以任何方式执行操作(可能通过隐藏包含源数据的工作表,然后锁定和密码保护文件)。 If there is a way to do it in Excel then you know the question to ask about how to do it in Python.如果有一种方法可以在 Excel 中执行此操作,那么您知道要询问如何在 Python 中执行此操作的问题。

As a suggestion, one way to do it could be to create an image of a chart and insert that into Excel.作为建议,一种方法是创建图表的图像并将其插入到 Excel 中。 That way there is no link to the original data.这样就没有原始数据的链接。

One option is to save the chart as an image and insert the image to your excel worksheet using xlsxwriter's insert_image一种选择是将图表保存为图像并使用 xlsxwriter 的 insert_image 将图像插入到 excel 工作表中

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保存 plot

Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig 使用 matplotlib 的 savefig 保存从 python pandas 生成的图(AxesSubPlot)

inserting the image插入图像

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用xlsxwriter将数据从Dataframe写入Excel工作表时出错 - Error writing data from Dataframe to excel sheet using xlsxwriter 使用Python从xml文件中提取数据并写入xlsxwriter - Extracting data from xml file using Python and writing to xlsxwriter Python:xlsxwriter 在没有条件的情况下按范围突出显示单元格 - Python: xlsxwriter highlight cells by range without condition 通过 xlsxwriter 添加 Sparkline 而无需在电子表格中写入数据 - add Sparkline by xlsxwriter without writing data in spreadsheet 有没有办法用不同的python函数从python中的用户那里获取一个函数(即如果使用个性化格式) - Is there a way to get a function from a user in python with different python functions(ie. if with personalized format) 有什么方法可以在不写入本地磁盘的情况下将 xlsxwriter 生成的文件发送到 azure 数据湖? - Any way to send an xlsxwriter generated file to azure data lake without writing to local disk? 没有图例的 XlsxWriter 图表? - XlsxWriter chart without the Legend? 使用python中的URL创建数据的任何方法 - Any way to create an excel sheet with data from a url with python 熊猫和xlsxwriter:如何在不导出数据框的情况下创建新工作表? - Pandas and xlsxwriter: how to create a new sheet without exporting a dataframe? 是否可以使用Xlsxwriter从Python的Excel工作表中读取数据? 如果可以,怎么办? - Is it possible to read data from an Excel sheet in Python using Xlsxwriter? If so how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM