目的

  掌握包 xlrd,pyga的简单使用

实现

excel file:

python生成分析图标(数据可根据代码自己调节)

代码

  import xlrd
  import pygal
  
  
  def main():
      # open excel file
      datax = xlrd.open_workbook("C:\\Users\\Davis\\Desktop\\123.xlsx")
  
      # get excel Sheets [0]: 0 first sheet,1 second sheet...
      sht = datax.sheets()[0]
      print(sht)
  
      # get sheets total Rows
      print(sht.nrows)
  
      # get sheets total Columns
      print(sht.ncols)
  
      # get row values
      print(sht.row_values(0))
      print(sht.row_values(1))
      print(sht.row_values(2))
  
      # get row values
      print(sht.col_values(0))
      print(sht.col_values(1))
      print(sht.col_values(2))
  
      # check all excel contend遍历表格内容
      for i in range(0, sht.nrows):
          print(sht.row_values(i))
  
      # thunder chart 雷达图
      chart = pygal.Radar()
  
      # add chart Title添加雷达标题
      chart.title = "进销存"
      
      # add title to chart
      chart.x_labels = chart.title[1:]
  
      # add data to chart 
      chart.add(sht.row_values(1)[0], sht.row_values(1)[1:])
      chart.add(sht.row_values(2)[0], sht.row_values(1)[1:])
      chart.add(sht.row_values(3)[0], sht.row_values(1)[1:])
      chart.add(sht.row_values(4)[0], sht.row_values(1)[1:])
      chart.add(sht.row_values(5)[0], sht.row_values(1)[1:])
      chart.add(sht.row_values(6)[0], sht.row_values(1)[1:])
  
      # 生成展示图表文件
      chart.render_to_file("chart.html")
  
  
  if __name__ == '__main__':
      main()