功能性的文章直接用幾個最簡單的實現表達:
xlsxwriter庫的核心就是其Workbook對象。
創建一個指定名字的xlsx文件:
1
2
3
4
5
6
|
import xlsxwriter filename = '/Users/piperck/Desktop/axiba.xlsx' test_book = xlsxwriter.Workbook(filename) worksheet = test_book.add_worksheet() test_book.close() |
創建一個Workbook的實例對象。可以傳入一個文件名字,如果不想生成的文件在當前路徑下面,可以在文件名字前面帶上絕對路徑。
add_worksheet()就是增加一個sheet
然后關閉這個對象,完成xlsx文件的生成。
創建一個指定名字的sheet并且為其添加一些數據:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import xlsxwriter filename = '/Users/piperck/Desktop/axiba.xlsx' test_book = xlsxwriter.Workbook(filename) worksheet = test_book.add_worksheet( 'what' ) expenses = ( [ 'Rent' , 1000 ], [ 'Gas' , 100 ], [ 'Food' , 300 ], [ 'Gym' , 50 ], ) # 定義起始的行列 會在這個基礎上 行列各加一 作為初始行列 row = 0 col = 0 for item, cost in expenses: worksheet.write(row, col, item) worksheet.write(row, col + 1 , cost) row + = 1 worksheet.write(row, col, '=sum(B0:B4)' ) test_book.close() |
我們可以使用得到的worksheet對象來添加其行列數據,如上所示。注意最后添加數據可以直接在第三個參數里面使用函數。
創建一個有指定樣式的Workbook:
這個方法其實。。應該有非常多的參數,大家根據實際需要可以具體去查詢更多的屬性。這個樣式要在Workbook的對象上加。
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
|
import xlsxwriter filename = '/Users/piperck/Desktop/axiba.xlsx' test_book = xlsxwriter.Workbook(filename) worksheet = test_book.add_worksheet( 'what' ) bold = test_book.add_format({ 'bold' : True }) test_book.add_format() expenses = ( [ 'Rent' , 1000 ], [ 'Gas' , 100 ], [ 'Food' , 300 ], [ 'Gym' , 50 ], ) # 定義起始的行列 會在這個基礎上 行列各加一 作為初始行列 row = 0 col = 0 for item, cost in expenses: worksheet.write(row, col, item, bold) worksheet.write(row, col + 1 , cost) row + = 1 test_book.close() |
關于更多的參數,完全可以參看源代碼里面的property字典下面初始化的那一堆東西,應該都是。
根絕著就能解決大部分問題了,如果有更多的需求就查閱下面的文檔即可。
通用做法可能會基于此再做一些東西來包裝 xlsxwriter 來讓他更好用,這個就看大家對自己業務需要抽象的能力了。
Reference:
https://xlsxwriter.readthedocs.io xlsxwriter doc
在當前文件夾生成
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#coding=utf-8 def get_excel(): """ 生成excel :return: """ import xlsxwriter workbook = xlsxwriter.Workbook( "test.xlsx" ) worksheet = workbook.add_worksheet() # 樣式 formats = Struct() # 字典轉化為點語法 formats.base = { "font_name" : u "宋體" , "font_size" : 11 , "align" : "center" , "valign" : "vcenter" , "text_wrap" : True } # formats.condition = dict_merge(formats.base, {"align": "left"}) formats.bold = { "bold" : True } # 加粗 formats.row = dict_merge(formats.base, { "border" : 1 }) formats.first_row = dict_merge(formats.row, { "bold" : True }) # 首行 formats.more_row = dict_merge(formats.row, {}) # 普通行 formats.more_row_even = dict_merge(formats.row, { "bg_color" : "#dddddd" }) # 普通行 - 奇數 # 篩選條件行 worksheet.merge_range( 'A1:F1' , "") # 合并單元格 conditions_list = [] # 條件 province = '省' city = '市' county = '地區' name = '姓名' phone = '電話' date = '2018-6' if province or city or county: area_name = province + city + county conditions_list.append(workbook.add_format(formats.bold)) conditions_list.append(u '地區:' ) conditions_list.append(u '%s ' % area_name) if name: conditions_list.append(workbook.add_format(formats.bold)) conditions_list.append(u '姓名:' ) conditions_list.append(u '%s ' % name) if phone: conditions_list.append(workbook.add_format(formats.bold)) conditions_list.append(u '手機:' ) conditions_list.append(u '%s ' % phone) if date: year, month = date[ 0 : 4 ], date[ 5 : 7 ] conditions_list.append(workbook.add_format(formats.bold)) conditions_list.append(u '創建時間:' ) conditions_list.append(u '%s/%s ' % (year, month)) if conditions_list: # 如果有條件 worksheet.write_rich_string( 'A1' , * conditions_list) # 首行 # 表格首行 cols = [ "姓名" , "電話" , "地區" ] for col_index, col in enumerate (cols): worksheet.write( 1 , col_index, col, workbook.add_format(formats.first_row)) # 第二行,col_index列, col_index從0開始,也就是第一列開始 data_list = [{ "name" : "Spencer" , "tel" : "13888888888" , "reg" : "中國" },{ "name" : "Jeff" , "tel" : "139999999999" , "reg" : "臺灣省" }] # 表格其余行 for row_index, u in enumerate (data_list, start = 2 ): # 因為前兩行都被占用了,所以從第三行第一列開始 # 斑馬條 if row_index % 2 ! = 0 : row_format = formats.more_row # excel格式普通行 else : row_format = dict_merge(formats.more_row_even) # excel格式奇數行 # 日期格式 date_format = dict_merge(row_format, { "num_format" : "yyyy/mm/dd hh:mm" }) # 靠左 left_format = dict_merge(row_format, { "align" : "left" }) # 第一個參數:行,第二個參數:列,第三個參數:數據,第四個參數:屬性 worksheet.write(row_index, 0 , u[ 'name' ], workbook.add_format(row_format)) worksheet.write(row_index, 1 , u[ 'tel' ], workbook.add_format(row_format)) worksheet.write(row_index, 2 , u[ 'reg' ], workbook.add_format(row_format)) # 列寬 # 第一個參數是第幾列開始,第二個人參數是從第幾列結束 # 比如下方第一個就是設置第一列為20,第二個就是設置第二列為10,第三個就是設置3到6列為20 worksheet.set_column( 0 , 0 , 20 ) worksheet.set_column( 1 , 1 , 10 ) worksheet.set_column( 2 , 5 , 20 ) workbook.close() def dict_merge( * args): """ 功能說明:合并字典 """ all = {} for arg in args: if not isinstance (arg, dict ): continue all .update(arg) return all class Struct( dict ): """ - 為字典加上點語法. 例如: >>> o = Struct({'a':1}) >>> o.a >>> 1 >>> o.b >>> None """ def __init__( self , dictobj = {}): self .update(dictobj) def __getattr__( self , name): # 如果有則返回值,沒有則返回None if name.startswith( '__' ): raise AttributeError return self .get(name) def __setattr__( self , name, val): self [name] = val def __hash__( self ): return id ( self ) if __name__ = = '__main__' : get_excel() |
到此這篇關于使用python庫xlsxwriter庫來輸出各種xlsx文件的示例的文章就介紹到這了,更多相關python xlsxwriter輸出xlsx內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/piperck/p/6363256.html