如何使用Openpyxl在Excel中使用行数据制作饼图?

huangapple go评论102阅读模式
英文:

How to make a pie chart with row data in Excel with Openpyxl?

问题

以下是您要翻译的内容:

  1. Hello, I am trying to make a pie chart with row data using openpyxl. I have found several examples with column data but I need row data. The example is this one:
  2. from openpyxl import Workbook
  3. from openpyxl.chart import (
  4. PieChart,
  5. ProjectedPieChart,
  6. Reference
  7. )
  8. from openpyxl.chart.series import DataPoint
  9. data = [
  10. ['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
  11. ['Sold', 50, 30, 10, 40]
  12. ]
  13. wb = Workbook()
  14. ws = wb.active
  15. for row in data:
  16. ws.append(row)
  17. pie = PieChart()
  18. labels = Reference(ws, min_col=1, min_row=1, max_col=5, max_row=1)
  19. data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
  20. pie.add_data(data, titles_from_data=True)
  21. pie.set_categories(labels)
  22. pie.title = "Pies sold by category"
  23. ws.add_chart(pie, "D1")

结果如图所示。图片

我已经尝试修改min_row/col和max_row/col,但无法找到解决方案。

英文:

`Hello, I am trying to make a pie chart wit row data using openpyxl. I have found several examples with column data but I need row data. The example is this one:

  1. from openpyxl import Workbook
  2. from openpyxl.chart import (
  3. PieChart,
  4. ProjectedPieChart,
  5. Reference
  6. )
  7. from openpyxl.chart.series import DataPoint
  8. data = [
  9. ['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
  10. ['Sold', 50, 30, 10 ,40]
  11. ]
  12. wb = Workbook()
  13. ws = wb.active
  14. for row in data:
  15. ws.append(row)
  16. pie = PieChart()
  17. labels = Reference(ws, min_col=1, min_row=1, max_col=5, max_row=1)
  18. data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
  19. pie.add_data(data, titles_from_data=True)
  20. pie.set_categories(labels)
  21. pie.title = "Pies sold by category"
  22. ws.add_chart(pie, "D1")

#The result that I obtain is shown in the picture.Picture

#I have tried to modify the min_row/col and max_row/col but I do not manage to get the solution.`

答案1

得分: 0

以下是您要翻译的代码部分:

  1. from openpyxl import Workbook
  2. from openpyxl.chart import (
  3. PieChart,
  4. ProjectedPieChart,
  5. Series, # <-- Add Series
  6. Reference
  7. )
  8. from openpyxl.chart.series import DataPoint
  9. data = [
  10. ['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
  11. ['Sold', 50, 30, 10, 40]
  12. ]
  13. wb = Workbook()
  14. ws = wb.active
  15. for row in data:
  16. ws.append(row)
  17. pie = PieChart()
  18. labels = Reference(ws, min_col=2, min_row=1, max_col=5, max_row=1)
  19. # data = Reference(ws, min_col=2, min_row=2, max_col=5, max_row=2)
  20. ### Expand range for label
  21. data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
  22. # Add chart Series
  23. # pie.add_data(data, titles_from_data=True)
  24. series = Series(data, title_from_data=True)
  25. pie.append(series)
  26. pie.set_categories(labels)
  27. pie.title = "Pies sold by category"
  28. ws.add_chart(pie, "D1")
  29. wb.save('pie_chart.xlsx')
英文:

In simple form add the data as a series;

  1. from openpyxl import Workbook
  2. from openpyxl.chart import (
  3. PieChart,
  4. ProjectedPieChart,
  5. Series, # &lt;-- Add Series
  6. Reference
  7. )
  8. from openpyxl.chart.series import DataPoint
  9. data = [
  10. [&#39;Pie&#39;, &#39;Apple&#39;, &#39;Cherry&#39;, &#39;Pumpkin&#39;, &#39;Chocolate&#39;],
  11. [&#39;Sold&#39;, 50, 30, 10, 40]
  12. ]
  13. wb = Workbook()
  14. ws = wb.active
  15. for row in data:
  16. ws.append(row)
  17. pie = PieChart()
  18. labels = Reference(ws, min_col=2, min_row=1, max_col=5, max_row=1)
  19. # data = Reference(ws, min_col=2, min_row=2, max_col=5, max_row=2)
  20. ### Expand range for label
  21. data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
  22. # Add chart Series
  23. # pie.add_data(data, titles_from_data=True)
  24. series = Series(data, title_from_data=True)
  25. pie.append(series)
  26. pie.set_categories(labels)
  27. pie.title = &quot;Pies sold by category&quot;
  28. ws.add_chart(pie, &quot;D1&quot;)
  29. wb.save(&#39;pie_chart.xlsx&#39;)

huangapple
  • 本文由 发表于 2023年3月4日 02:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定