Advertisement
skip420

Pie_Chart_2_spread_sheet

Aug 30th, 2020
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. # import xlsxwriter module
  2. import xlsxwriter
  3.  
  4. # Workbook() takes one, non-optional, argument
  5. # which is the filename that we want to create.
  6. workbook = xlsxwriter.Workbook('chart_pie.xlsx')
  7.  
  8. # The workbook object is then used to add new
  9. # worksheet via the add_worksheet() method.
  10. worksheet = workbook.add_worksheet()
  11.  
  12. # Create a new Format object to formats cells
  13. # in worksheets using add_format() method .
  14.  
  15. # here we create bold format object .
  16. bold = workbook.add_format({'bold': 1})
  17.  
  18. # create a data list .
  19. headings = ['Category', 'Values']
  20.  
  21. data = [
  22. ['Students Gain', 'Students Loss', 'Goal'],
  23. [200, 15,500],
  24. ]
  25.  
  26. # Write a row of data starting from 'A1'
  27. # with bold format.
  28. worksheet.write_row('A1', headings, bold)
  29.  
  30. # Write a column of data starting from
  31. # A2, B2, C2 respectively.
  32. worksheet.write_column('A2', data[0])
  33. worksheet.write_column('B2', data[1])
  34.  
  35. # Create a chart object that can be added
  36. # to a worksheet using add_chart() method.
  37.  
  38. # here we create a pie chart object .
  39. chart1 = workbook.add_chart({'type': 'pie'})
  40.  
  41. # Add a data series to a chart
  42. # using add_series method.
  43. # Configure the first series.
  44. #[sheetname, first_row, first_col, last_row, last_col].
  45. chart1.add_series({
  46. 'name': 'Pie sales data',
  47. 'categories': ['Sheet1', 1, 0, 3, 0],
  48. 'values': ['Sheet1', 1, 1, 3, 1],
  49. })
  50.  
  51. # Add a chart title
  52. chart1.set_title({'name': 'Popular Pie Types'})
  53.  
  54. # Set an Excel chart style. Colors with white outline and shadow.
  55. chart1.set_style(10)
  56.  
  57. # Insert the chart into the worksheet(with an offset).
  58. # the top-left corner of a chart is anchored to cell C2.
  59. worksheet.insert_chart('C2', chart1, {'x_offset': 25, 'y_offset': 10})
  60.  
  61. # Finally, close the Excel file
  62. # via the close() method.
  63. workbook.close()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement