python - 使用python在excel中生成图形

标签 python excel charts export-to-excel

我一直在尝试在 Excel 中生成数据。 我生成了 .CSV 文件。 所以到那时它很容易。 但是在 Excel 中生成图形非常困难...

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

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

最佳答案

是的,Xlsxwriter[ docs ][ pypi ] 有很多用于创建 excel 的实用程序 charts在 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

output of exmaple

关于python - 使用python在excel中生成图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568319/

相关文章:

r - 在 ggplot 中插入值的百分比

arrays - Excel VBA 连接函数

excel - 复制一系列单元格并仅选择包含数据的单元格

charts - Google Charts - HTML 轴标签和标题

javascript - 在谷歌图表数据表中插入行和列

r - Excel中的数字列被转换为逻辑

python - 使用带有多个参数的 fromfile_prefix_chars (nargs ="*")

python - Sympy 中 S 的含义

python - 从 Seaborn Boxplot 中提取异常值

python - 在python中从http服务器下载文件