經過python中xlrd讀取excel表格(xlwt寫入excel),xlsxwriter寫入excel表格並繪製圖形

 1 import xlrd, xlwt
 2 
 3 #讀取excel文件
 4 def read_excel(url):#傳入源文件讀取路徑
 5     # 獲取數據
 6     data = xlrd.open_workbook(url)
 7     # 獲取sheet
 8     # table = data.sheet_by_name(sheet_name) #經過sheet名稱獲取sheet數據
 9     table = data.sheet_by_index(0) #經過sheet索引獲取sheet數據
10     # 獲取總行數
11     nrows = table.nrows
12     # 獲取總列數
13     ncols = table.ncols
14     # 獲取一個單元格的數值
15     cell_value = table.cell(0, 1).value #獲取第0行第1列的值,即 B1 的值
16     print(cell_value)
17     
18 #寫入excel文件
19 def write_excel(url, sheet_name, data): #傳入文件存儲路徑、excel的sheet名稱、以及要插入的數據
20     #data = (('biqi', 963, 177), ('editor_Intern1', 912, 154), ('editor_Intern10', 840, 163), ('editor_Intern11', 644, 173)) #模板數據
21     myWorkbook = xlwt.Workbook() #建立excel
22     mySheet = myWorkbook.add_sheet(sheet_name) #建立excel中的sheet
23     for i, val in enumerate(data):
24         for j, value in enumerate(val):
25             mySheet.write(i, j, value) #遍歷數據插入sheet中
26     myWorkbook.save(url) #將建立的excel保存在該路徑下
  1 import xlsxwriter
  2 
  3 #寫入excel文件
  4 def write_excel(url): #傳入excel存儲路徑
  5     #將要插入excel並用做繪圖的數據
  6     data_all = {'錯誤彙總': {
  7         'error_summary_header': ['帳號', '姓名', '投資評級錯誤', '評級變化錯誤', '股票代碼錯誤', '股票名稱錯誤', '目標價錯誤', '目標價高錯誤', '淨利潤錯誤',
  8                                  '歸母淨利潤錯誤', '年份錯誤', '分析師名字錯誤', '郵箱錯誤', '證券職業編碼錯誤', '電話錯誤', '記錄錯誤數', '錯誤數(人工)',
  9                                  '錯誤數(研報自己)', '錯誤數(抽查)', '總記錄數', '錯誤率', '研報總數'],
 10         'editor_Intern1': ['editor_Intern1', 'editor_Intern1', 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 912, '0.22%',
 11                            223],
 12         'editor_Intern10': ['editor_Intern10', 'editor_Intern10', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 837, '0.12%',
 13                             232],
 14         'editor_Intern11': ['editor_Intern11', 'editor_Intern11', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 644, '0.16%',
 15                             173],
 16         'editor_Intern12': ['editor_Intern12', 'editor_Intern12', 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 439, '0.23%',
 17                             99],
 18         'editor_Intern2': ['editor_Intern2', 'editor_Intern2', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1132, '0.0%',
 19                            265],
 20         'editor_Intern3': ['editor_Intern3', 'editor_Intern3', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 852, '0.0%', 280],
 21         'editor_Intern4': ['editor_Intern4', 'editor_Intern4', 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 1002, '0.7%',
 22                            297],
 23         'editor_Intern5': ['editor_Intern5', 'editor_Intern5', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 571, '0.0%',
 24                            213],
 25         'editor_Intern6': ['editor_Intern6', 'editor_Intern6', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 778, '0.0%',
 26                            200],
 27         'editor_Intern8': ['editor_Intern8', 'editor_Intern8', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 28         'editor_Intern9': ['editor_Intern9', 'editor_Intern9', 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 762, '0.66%', -3],
 29         'editor_Intern13': ['editor_Intern13', 'editor_Intern13', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 729, '0.0%',
 30                             180],
 31         'editor_Intern14': ['editor_Intern14', 'editor_Intern14', 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 2, 0, 0, 10, 0, 0, 0, 467, '2.14%',
 32                             102],
 33         'Total': ['Total', '合計', 0, 0, 0, 0, 0, 0, 12, 3, 9, 0, 4, 0, 0, 28, 0, 0, 0, 10102, '0.28%', 2595]}}
 34     # 數據表頭
 35     error_detail_header = ['研報_id', '帳號', '投資評級錯誤', '評級變化錯誤', '股票代碼錯誤', '股票名稱錯誤', '目標價錯誤', '目標價高錯誤', '淨利潤錯誤', '歸母淨利潤錯誤',
 36                            '年份錯誤', '分析師名字錯誤', '郵箱錯誤', '證券職業編碼錯誤', '電話錯誤']
 37     #建立excel
 38     myWorkbook = xlsxwriter.Workbook(url)
 39     # 自定義樣式
 40     bold = myWorkbook.add_format({
 41         'font_size': 10,  # 字體大小
 42         'bold': True,  # 是否粗體
 43         'bg_color': '#101010',  # 表格背景顏色
 44         'font_color': '#FEFEFE',  # 字體顏色
 45         'align': 'center',  # 居中對齊
 46         'top': 2,  # 上邊框
 47         'left': 2,  # 左邊框
 48         'right': 2,  # 右邊框
 49         'bottom': 2  # 底邊框
 50     })
 51     for k, v in data_all.items():
 52         if k == '錯誤明細':
 53             mySheet1 = myWorkbook.add_worksheet(k) #建立「錯誤明細」sheet
 54             for index, header in enumerate(error_detail_header):
 55                 mySheet1.write(0, index, header, bold)
 56             for i, val in enumerate(v):
 57                 i += 1
 58                 for j, value in enumerate(val):
 59                     mySheet1.write(i, j, value, bold) #向第i行第j列插入數據,並使用bold定義的樣式
 60         if k == '錯誤彙總':
 61             mySheet2 = myWorkbook.add_worksheet(k)
 62             i = 0
 63             for summary_value in v.values():
 64                 for sum_index, sum_value in enumerate(summary_value):
 65                     mySheet2.write(i, sum_index, sum_value, bold)
 66                 i += 1
 67 
 68     '''繪製錯誤數柱狀圖'''
 69     # 建立一個柱狀圖(column chart)
 70     chart_col = myWorkbook.add_chart({'type': 'column'})
 71 
 72     # 圖表下方顯示數據表格
 73     chart_col.set_table({
 74         'show_keys': True
 75     })
 76 
 77     # 配置數據(用了另外一種語法)
 78     chart_col.add_series({
 79         'name': '=錯誤彙總!$P$1',
 80         'categories': '=錯誤彙總!$B$2:$B$14',
 81         'values': '=錯誤彙總!$P$2:$P$14',
 82         'line': {'color': '#C0504D'},
 83         'fill': {'color': '#C0504D'},
 84         'data_labels': {'value': True}, #在圖表上顯示對應的數據
 85     })
 86 
 87     # # 配置數據
 88     # chart_col.add_series({
 89     #     'name': ['錯誤彙總', 0, 2],
 90     #     'categories': ['錯誤彙總', 1, 0, 6, 0],
 91     #     'values': ['錯誤彙總', 1, 2, 6, 2],
 92     #     'line': {'color': 'red'},
 93     # })
 94 
 95     # 設置圖表的title 和 x,y軸信息
 96     chart_col.set_title({'name': '記錄錯誤數'})
 97     chart_col.set_x_axis({'name': '員工'})
 98     chart_col.set_y_axis({'name': '錯誤數'})
 99 
100     # 設置圖表的風格
101     chart_col.set_style(10)
102 
103     # 把圖表插入到worksheet以及偏移
104     mySheet2.insert_chart('A21', chart_col, {
105         'x_offset': 0,
106         'y_offset': 0,
107         'x_scale':  1.5,
108         'y_scale':  1.5,
109     }) #第一個參數爲圖表插入的起始位置, x_offset、y_offset爲偏移量, x_scale、y_scale爲縮放比率
110 
111     '''繪製錯誤率折線圖'''
112     chart_col1 = myWorkbook.add_chart({'type': 'line'})
113     # 配置數據(用了另外一種語法)
114     chart_col1.add_series({
115         'name': '=錯誤彙總!$U$1',
116         'categories': '=錯誤彙總!$B$2:$B$14',
117         'values': '=錯誤彙總!$U$2:$U$14',
118         'line': {'color': '#C0504D'},
119         'data_labels': {'value': True},
120     })
121 
122     # # 配置數據
123     # chart_col1.add_series({
124     #     'name': ['錯誤彙總', 0, 2],
125     #     'categories': ['錯誤彙總', 1, 0, 6, 0],
126     #     'values': ['錯誤彙總', 1, 2, 6, 2],
127     #     'line': {'color': 'red'},
128     # })
129 
130     # 設置圖表的title 和 x,y軸信息
131     chart_col1.set_title({'name': '錯誤率'})
132     chart_col1.set_x_axis({'name': '員工'})
133     chart_col1.set_y_axis({'name': '錯誤數'})
134 
135     # 設置圖表的風格
136     chart_col1.set_style(1)
137 
138     # 將柱狀圖合併入折線圖中
139     # chart_col1.combine(chart_col)
140 
141     mySheet2.insert_chart('N21', chart_col1, {
142         'x_offset': 0,
143         'y_offset': 0,
144         'x_scale': 1.5,
145         'y_scale': 1.5,
146     }) #第一個參數爲圖表插入的起始位置, x_offset、y_offset爲偏移量, x_scale、y_scale爲縮放比率
147 
148     '''繪製錯誤種類餅圖'''
149     # 建立一個柱狀圖(column chart)
150     chart_col2 = myWorkbook.add_chart({'type': 'pie'})
151     # 配置數據(用了另外一種語法)
152     chart_col2.add_series({
153         'categories': '=錯誤彙總!$C$1:$O$1',
154         'values': '=錯誤彙總!$C$15:$O$15',
155         'data_labels': {'value': True},
156         'points': [
157             {'fill': {'color': '#4590A7'}},
158             {'fill': {'color': '#AA4643'}},
159             {'fill': {'color': '#89A54E'}},
160             {'fill': {'color': '#71588F'}},
161             {'fill': {'color': '#4198AF'}},
162             {'fill': {'color': '#DB843D'}},
163             {'fill': {'color': '#93A9CF'}},
164             {'fill': {'color': '#D19392'}},
165             {'fill': {'color': '#B9CD96'}},
166             {'fill': {'color': '#4590A7'}},
167             {'fill': {'color': '#AA4643'}},
168             {'fill': {'color': '#89A54E'}},
169             {'fill': {'color': '#71588F'}},
170             {'fill': {'color': '#4198AF'}},
171             {'fill': {'color': '#DB843D'}},
172             {'fill': {'color': '#93A9CF'}},
173             {'fill': {'color': '#D19392'}},
174             {'fill': {'color': '#B9CD96'}},
175         ] #餅狀圖會使用到的色號
176     })
177 
178     # # 配置數據
179     # chart_col.add_series({
180     #     'name': ['錯誤彙總', 0, 2],
181     #     'categories': ['錯誤彙總', 1, 0, 6, 0],
182     #     'values': ['錯誤彙總', 1, 2, 6, 2],
183     #     'line': {'color': 'red'},
184     # })
185 
186     # 設置圖表的title
187     chart_col2.set_title({'name': '錯誤種類佔比'})
188 
189     # 設置圖表的風格
190     chart_col2.set_style(1)
191 
192     # 把圖表插入到worksheet以及偏移
193     mySheet2.insert_chart('H45', chart_col2, {
194         'x_offset': 0,
195         'y_offset': 0,
196         'x_scale': 1.5,
197         'y_scale': 1.5,
198     }) #第一個參數爲圖表插入的起始位置, x_offset、y_offset爲偏移量, x_scale、y_scale爲縮放比率
199 
200     myWorkbook.close()
201 
202 if __name__ == '__main__':
203     write_excel('./datasource/test_query_bak.xls')

 整體來講,若是隻須要從excel讀取數據及向excel中插入數據,用xlrd包及xlwt包就能夠很好的解決了;但若是想要在excel中繪製各類報表圖片就須要藉助xlsxwriter包來進行了(xlsxwriter官網:http://xlsxwriter.readthedocs.io/chart.html)html

附上xlsxwriter繪製的圖表的截圖字體

相關文章
相關標籤/搜索