1+ from openpyxl import Workbook
2+
3+ def createWorkbook ():
4+ # 创建一个 workbook 对象
5+ wb = Workbook ()
6+ # 得到激活的 sheet
7+ ws = wb .active
8+ # 单元个设置值
9+ ws ['A1' ] = 42
10+ # 批量设置行
11+ ws .append ([1 , 2 , 3 ])
12+
13+ wb .save ('createWorkbook.xlsx' )
14+
15+ def loadWorkbook ():
16+ from openpyxl import load_workbook
17+ wb = load_workbook ('sample.xlsx' )
18+ print ("sample.xlsx 活动 sheet 第一个单元格值为:" , wb .active ['A1' ].value )
19+
20+ def operateCell ():
21+ wb = Workbook ()
22+ ws = wb .active
23+ ws .append ((1 ,2 ,3 ))
24+ ws .append ((11 ,22 ,33 ))
25+ ws .append ((111 ,222 ,333 ))
26+
27+ # 操作单列
28+ for cell in ws ["A" ]:
29+ print (cell .value )
30+ # 操作单行
31+ for cell in ws ["1" ]:
32+ print (cell .value )
33+ # 操作多列
34+ for column in ws ['A:C' ]:
35+ for cell in column :
36+ print (cell .value )
37+ # 操作多行
38+ for row in ws ['1:3' ]:
39+ for cell in row :
40+ print (cell .value )
41+ # 指定范围
42+ for row in ws ['A1:C3' ]:
43+ for cell in row :
44+ print (cell .value )
45+
46+ def setCellFormat ():
47+ # 单元格格式
48+ from openpyxl .styles import Font , PatternFill , Border , Side , Alignment , Protection
49+ from openpyxl .styles import numbers
50+
51+ wb = Workbook ()
52+ ws = wb .active
53+ ws .cell (row = 1 , column = 1 , value = '宋体' ).font = Font (name = u'宋体' , size = 12 , bold = True , color = 'FF0000' )
54+ ws .cell (row = 2 , column = 2 , value = '右对齐' ).alignment = Alignment (horizontal = 'right' )
55+ ws .cell (row = 3 , column = 3 , value = '填充渐变色' ).fill = PatternFill (fill_type = 'solid' , start_color = 'FF0000' )
56+ ws .cell (row = 4 , column = 4 , value = '设置边线' ).border = Border (left = Side (border_style = 'thin' , color = 'FF0000' ), right = Side (border_style = 'thin' , color = 'FF0000' ))
57+ ws .cell (row = 5 , column = 5 , value = '受保护的' ).protection = Protection (locked = True , hidden = True )
58+ ws .cell (row = 6 , column = 6 , value = 0.54 ).number_format = numbers .FORMAT_PERCENTAGE
59+ wb .save ('setCellFormat.xlsx' )
60+ print ('打开文件 setCellFormat.xlsx 查看结果' )
61+
62+ def barChart ():
63+ from openpyxl import Workbook
64+ from openpyxl .chart import BarChart , Reference
65+
66+ wb = Workbook ()
67+ ws = wb .active
68+
69+ rows = [
70+ ('月份' , '苹果' , '香蕉' ),
71+ (1 , 43 , 25 ),
72+ (2 , 10 , 30 ),
73+ (3 , 40 , 60 ),
74+ (4 , 50 , 70 ),
75+ (5 , 20 , 10 ),
76+ (6 , 10 , 40 ),
77+ (7 , 50 , 30 ),
78+ ]
79+
80+ for row in rows :
81+ ws .append (row )
82+
83+ chart1 = BarChart ()
84+ chart1 .type = "col"
85+ chart1 .style = 10
86+ chart1 .title = "销量柱状图"
87+ chart1 .y_axis .title = '销量'
88+ chart1 .x_axis .title = '月份'
89+
90+ data = Reference (ws , min_col = 2 , min_row = 1 , max_row = 8 , max_col = 3 )
91+ series = Reference (ws , min_col = 1 , min_row = 2 , max_row = 8 )
92+ chart1 .add_data (data , titles_from_data = True )
93+ chart1 .set_categories (series )
94+ ws .add_chart (chart1 , "A10" )
95+
96+ wb .save ('barChart.xlsx' )
97+ print ('打开文件 barChart.xlsx 查看结果' )
98+
99+ def pieChart ():
100+ from openpyxl import Workbook
101+ from openpyxl .chart import PieChart , Reference
102+
103+ data = [
104+ ['水果' , '销量' ],
105+ ['苹果' , 50 ],
106+ ['樱桃' , 30 ],
107+ ['橘子' , 10 ],
108+ ['香蕉' , 40 ],
109+ ]
110+
111+ wb = Workbook ()
112+ ws = wb .active
113+
114+ for row in data :
115+ ws .append (row )
116+
117+ pie = PieChart ()
118+ labels = Reference (ws , min_col = 1 , min_row = 2 , max_row = 5 )
119+ data = Reference (ws , min_col = 2 , min_row = 1 , max_row = 5 )
120+ pie .add_data (data , titles_from_data = True )
121+ pie .set_categories (labels )
122+ pie .title = "水果销量占比"
123+
124+ ws .add_chart (pie , "D1" )
125+ wb .save ('piechart.xlsx' )
126+ print ('打开文件 piechart.xlsx 查看结果' )
127+
128+ if __name__ == '__main__' :
129+ createWorkbook ()
130+ loadWorkbook ()
131+ operateCell ()
132+ setCellFormat ()
133+ barChart ()
134+ pieChart ()
0 commit comments