python編寫excel周報統計圖表

來源:互聯網
上載者:User

標籤:python編寫excel周報統計圖表

650) this.width=650;" width="805" height="456" src="https://wuguiyunwei.com/wp-content/uploads/2017/04/py-execl.png" class="attachment-full size-full wp-post-image" style="border:none;vertical-align:middle;height:auto;width:770px;" />

  1. 安裝模組 :pip install XlsxWriter
  2. 直接直接複製以下代碼 儲存檔案名稱為wuguiyunwei.py
  3. 執行:python wuguiyunwei.py

註:可以根據業務需求自行修改所要參數

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

#!/usr/bin/env python

#coding: utf-8

import xlsxwriter

 

workbook = xlsxwriter.Workbook(‘wugui.xlsx‘) #建立一個Excel檔案

worksheet = workbook.add_worksheet()  #建立一個工作表對象

chart = workbook.add_chart({‘type‘:‘column‘}) #建立一個圖表對象

#定義資料表開頭列表

title = [u‘業務名稱‘,u‘星期一‘,u‘星期二‘,u‘星期三‘,u‘星期四‘,u‘星期五‘,u‘星期六‘,u‘星期日‘,u‘平均流量‘]

buname = [u‘烏龜營運官網‘,u‘烏龜1‘,u‘烏龜2‘,u‘烏龜3‘,u‘烏龜4‘] #定義頻道名稱

#定義5頻道一周七天資料列表

data = [

    [134,432,348,349,565,435,158],

    [176,168,94,53,68,700,91],

    [251,265,154,165,540,154,135],

    [77,76,75,74,73,73],

    [81,82,83,84,91,83,82],

]

format=workbook.add_format() #定義format格式對象

format.set_border(1) #定義format對象儲存格邊框加粗(1像素)的格式

 

format_title=workbook.add_format() #定義format_title格式對象

format_title.set_border(1) #定義format_title對象儲存格邊框加粗(1像素)的格式

format_title.set_bg_color(‘#cccccc‘) #定義format_title對象儲存格背景顏色為‘cccccc’的格式

format_title.set_align(‘center‘) #定義format_title對象儲存格劇中對齊的格式

format_title.set_bold() #定義format_title對象儲存格內容加粗的格式

 

format_ave=workbook.add_format() #定義format_ave格式對象

format_ave.set_border(1) #定義format_ave對象儲存格邊框加粗(1像素)的格式

format_ave.set_num_format(‘0.00‘) #定義format_ave對象儲存格數字類別顯示格式

 

#下面分別以行或列寫入方式將標題,業務名稱,流量資料寫入起初儲存格,同時引用不同格式對象

worksheet.write_row(‘A1‘,title,format_title)

worksheet.write_column(‘A2‘,buname,format)

worksheet.write_row(‘B2‘,data[0],format)

worksheet.write_row(‘B3‘,data[1],format)

worksheet.write_row(‘B4‘,data[2],format)

worksheet.write_row(‘B5‘,data[3],format)

worksheet.write_row(‘B6‘,data[4],format)

 

#定義圖表資料數列函數

def chart_series(cur_row):

    worksheet.write_formula(‘I‘+cur_row,‘=AVERAGE(B‘+cur_row+‘:H‘+cur_row+‘)‘,format_ave) #計算(AVERAGE函數)

   #頻道周平均流量

    chart.add_series({

        ‘categories‘:‘=Sheet1!$B$1:$H$1‘, #將“星期一至星期日”作為表徵圖資料標籤(X軸)

‘values‘:      ‘=Sheet1!$B$‘+cur_row+‘:$H$‘+cur_row, #頻道一周所有資料操作 為資料區域

‘line‘:        {‘color‘:‘black‘}, #線條顏色定義為black(黑色)

‘name‘:‘Sheet1!$A$‘+cur_row,

   })

for row in range(2,7): #資料域以第2~6行進行圖表資料數列函數調用

    chart_series(str(row))

#chart.set_table() #設定X軸表格格式,本樣本不啟用

#chart.set_style(30) #設定圖表樣式,本樣本不啟用

chart.set_size({‘width‘: 577,‘height‘:287}) #設定圖表大小

chart.set_title({‘name‘: u‘業務流量周報報表‘}) #設定圖表(上方)大標題

chart.set_y_axis({‘name‘: ‘MB/s‘}) #設定Y軸(左側)小表徵圖

worksheet.insert_chart(‘A8‘, chart) #在A8儲存格插入圖表

workbook.close() #關閉Excel文檔

 

我們的公用號

來自烏龜營運 wuguiyunwei.com

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/99/09/wKioL1lDSq_z5BdSAACeGDQ6kF0764.jpg" title="wg2weima.jpg" alt="wKioL1lDSq_z5BdSAACeGDQ6kF0764.jpg" />

python編寫excel周報統計圖表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.