使用python在excel中生成图表

发布于 2024-10-30 18:37:49 字数 218 浏览 2 评论 0原文

我一直在尝试在 Excel 中生成数据。 我生成了 .CSV 文件。 所以到目前为止,这很容易。 但是在Excel中生成图表非常困难...

我想知道,python是否能够在Excel中生成数据和图表? 如果有示例或代码片段,请随时发布:)

或者解决方法可以使用 python 生成图形格式的图形,如 .jpg 等,或者 .pdf 文件也可以..只要解决方法不需要依赖如需要安装boost库。

I have been trying to generate data in Excel.
I generated .CSV file.
So up to that point it's easy.
But generating graph is quite hard in Excel...

I am wondering, is python able to generate data AND graph in excel?
If there are examples or code snippets, feel free to post it :)

Or a workaround can be use python to generate graph in graphical format like .jpg, etc or .pdf file is also ok..as long as workaround doesn't need dependency such as the need to install boost library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

喵星人汪星人 2024-11-06 18:37:50

您有 2 个选择:

如果您使用的是 Windows,则可以使用 pywin32 (包含在 ActivePython) 库,使用 OLE 自动化

from win32com.client import Dispatch
ex = Dispatch("Excel.Application")
# you can use the ex object to invoke Excel methods etc.

如果您只想生成基本绘图等,您可以使用 matplotlib

You have 2 options:

If you are on windows, you can use pywin32 (included in ActivePython) library to automate Excel using OLE automation.

from win32com.client import Dispatch
ex = Dispatch("Excel.Application")
# you can use the ex object to invoke Excel methods etc.

If all you want to just generate basic plots etc. you can use matplotlib.

香草可樂 2024-11-06 18:37:50

我建议您在从数据文件绘制图形时尝试 gnuplot。

I suggest you to try gnuplot while drawing graph from data files.

美男兮 2024-11-06 18:37:50

如果您决定使用 matplotlib,请查看我的 excel 到 python 类 PyWorkbooks 来获取数据。它可以让您高效、轻松地以 numpy 数组(matplotlib 的本机数据类型)形式检索数据。

https://sourceforge.net/projects/pyworkbooks/

If you do decide to use matplotlib, check out my excel to python class PyWorkbooks to get the data. It lets you retrieve data efficiently and easily as numpy arrays (the native datatype for matplotlib).

https://sourceforge.net/projects/pyworkbooks/

祁梦 2024-11-06 18:37:50

@David Gau,我正在考虑做类似的事情。目前我正在考虑使用原始 csv 或将其转换为 json 并将其放入 jqplot 正在读取的文件夹中。 jquery 绘图和图形库。然后我需要做的就是让用户或我自己在任何网络浏览器中显示该图。

@David Gao, I am looking at doing something similar. Currently I am looking at using the raw csv or converting it to json and just dropping it in a folder that is being read by jqplot.jquery plotting and graphing library. Then all I need to do is have the user or myself display the plot in any web browser.

迷离° 2024-11-06 18:37:49

是的,Xlsxwriter[文档][pypi] 有很多用于创建 excel 的实用程序Python 中的图表。但是,您将需要使用 xlsx 文件格式,对于不正确的参数没有太多反馈,并且您无法读取输出。

import xlsxwriter
import random
# Example data
# Try to do as much processing outside of initializing the workbook
# Everything beetween Workbook() and close() gets trapped in an exception
random_data = [random.random() for _ in range(10)]
# Data location inside excel
data_start_loc = [0, 0] # xlsxwriter rquires list, no tuple
data_end_loc = [data_start_loc[0] + len(random_data), 0]

workbook = xlsxwriter.Workbook('file.xlsx')

# Charts are independent of worksheets
chart = workbook.add_chart({'type': 'line'})
chart.set_y_axis({'name': 'Random jiggly bit values'})
chart.set_x_axis({'name': 'Sequential order'})
chart.set_title({'name': 'Insecure randomly jiggly bits'})

worksheet = workbook.add_worksheet()

# A chart requires data to reference data inside excel
worksheet.write_column(*data_start_loc, data=random_data)
# The chart needs to explicitly reference data
chart.add_series({
    'values': [worksheet.name] + data_start_loc + data_end_loc,
    'name': "Random data",
})
worksheet.insert_chart('B1', chart)

workbook.close()  # Write to file

输出例如枫

Yes, Xlsxwriter[docs][pypi] has a lot of utility for creating excel charts in Python. However, you will need to use the xlsx file format, there is not much feedback for incorrect parameters, and you cannot read your output.

import xlsxwriter
import random
# Example data
# Try to do as much processing outside of initializing the workbook
# Everything beetween Workbook() and close() gets trapped in an exception
random_data = [random.random() for _ in range(10)]
# Data location inside excel
data_start_loc = [0, 0] # xlsxwriter rquires list, no tuple
data_end_loc = [data_start_loc[0] + len(random_data), 0]

workbook = xlsxwriter.Workbook('file.xlsx')

# Charts are independent of worksheets
chart = workbook.add_chart({'type': 'line'})
chart.set_y_axis({'name': 'Random jiggly bit values'})
chart.set_x_axis({'name': 'Sequential order'})
chart.set_title({'name': 'Insecure randomly jiggly bits'})

worksheet = workbook.add_worksheet()

# A chart requires data to reference data inside excel
worksheet.write_column(*data_start_loc, data=random_data)
# The chart needs to explicitly reference data
chart.add_series({
    'values': [worksheet.name] + data_start_loc + data_end_loc,
    'name': "Random data",
})
worksheet.insert_chart('B1', chart)

workbook.close()  # Write to file

output of exmaple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文