处理Excel电子表格

一个Excel电子表格文档称为一个工作薄。

每个工作薄可以包含多个工作表。

用户当前查看的表,称为活动表。


python没有自带openpyxl,所以必须安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
c:\python\Scripts>pip3.6 install openpyxl
Collecting openpyxl
   Downloading openpyxl-2.4.9.tar.gz (157kB)
     100% |████████████████████████████████| 163kB 183kB/s
Collecting jdcal (from openpyxl)
   Downloading jdcal-1.3.tar.gz
Collecting et_xmlfile (from openpyxl)
   Downloading et_xmlfile-1.0.1.tar.gz
Installing collected packages: jdcal, et-xmlfile, openpyxl
   Running setup.py install for jdcal ... done
   Running setup.py install for et-xmlfile ... done
   Running setup.py install for openpyxl ... done
Successfully installed et-xmlfile-1.0.1 jdcal-1.3 openpyxl-2.4.9
c:\python>python.exe
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl
>>>


QQ图片20171214210330.png


用openpyxl模块打开Excel文档

1
2
3
4
>>>  import  openpyxl
>>> wb = openpyxl.load_workbook( 'example.xlsx' )
>>>  type (wb)
< class  'openpyxl.workbook.workbook.Workbook' >

openpyxl.load_workbook()函数接受文件名,返回一个workbook数据类型的值。这个workbook对象代表这个Excel文件。

example.xlsx需要在当前工作目录,才能处理它。可以导入os,使用函数os.getcwd()确定当前工作目录,并用os.chdir()改变当前工作目录。


从工作薄中取得工作表

调用get_sheet_names()方法可以取得工作薄中所有表名的列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>>  import  openpyxl
>>> wb = openpyxl.load_workbook( 'example.xlsx' )
>>> wb.get_sheet_names()
[ 'Sheet1' 'Sheet2' 'Sheet3' ]
>>> sheet = wb.get_sheet_by_name( 'Sheet3' )
>>> sheet
<Worksheet  "Sheet3" >
>>>  type (sheet)
< class  'openpyxl.worksheet.worksheet.Worksheet' >
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.get_active_sheet()
>>> anotherSheet
<Worksheet  "Sheet3" >

每个表由一个Worksheet对象表示,可以通过向工作薄方法get_sheet_by_name()传递表名字符串获得。

调用Workbook对象的get_active_sheet()方法,取得工作薄的活动表。


从表中取得单元格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>>  import  openpyxl
>>> wb = openpyxl.load_workbook( 'example.xlsx' )
>>> sheet = wb.get_sheet_by_name( 'Sheet1' )
>>> sheet[ 'A1' ]
<Cell  'Sheet1' .A1>
>>> sheet[ 'A1' ].value = 'apple'
>>> sheet[ 'A1' ].value
'apple'
>>> c = sheet[ 'B1' ]
>>> c.value = 'juice'
>>>  str (c.row)
'1'
>>> c.row
1
>>> c.column
'B'
>>> c.coordinate
'B1'

Cell对象的value属性,包含这个单元格中保存的值。

Cell对象也有row、column和coordinate属性,提供该单元格的位置信息。

第一行或第一列的整数是1,不是0。

1
2
3
4
5
6
7
8
9
10
11
>>> sheet.cell(row = 1 ,column = 2 )
<Cell  'Sheet1' .B1>
>>> sheet.cell(row = 1 ,column = 2 ).value
'juice'
>>>  for  in  range ( 1 , 8 , 2 ):
...      print (i,sheet.cell(row = i,column = 2 ).value)
...
1  juice
3  None
5  None
7  None

可以通过Worksheet对象的get_highest_row()和get_highest_column()方法,确定表的大小。

get_highest_column()方法返回一个整数,而不是Excel中出现的字母。


列字母和数字之间的转换

要从字母转换到数字,就调用openpyxl.cell.column_index_from_string()函数。

要从数字转换到字母,就调用openpyxl.cell.get_column_letter()函数。


从表中取得行和列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>>  import  openpyxl
>>> wb = openpyxl.load_workbook( 'example.xlsx' )
>>> sheet = wb.get_sheet_by_name( 'Sheet1' )
>>>  tuple (sheet[ 'A1' : 'C3' ])
((<Cell  'Sheet1' .A1>, <Cell  'Sheet1' .B1>, <Cell  'Sheet1' .C1>), (<Cell  'Sheet1' .A2>, <Cell  'Sheet1' .B2>, <Cell  'Sheet1' .C2>), (<Cell  'Sheet1' .A3>, <Cell  'Sheet1' .B3>, <Cell  'Sheet1' .C3>))
>>>  for  in  sheet[ 'A1' : 'C3' ]:
...      for  in  i:
...              print (j.coordinate,j.value)
...      print ( '--- END OF ROW ---' )
...
A1 apple
B1 juice
C1 cake
- - -  END OF ROW  - - -
A2  None
B2 nurse
C2  None
- - -  END OF ROW  - - -
A3  None
B3  None
C3  None
- - -  END OF ROW  - - -


总结:

1、导入openpyxl模块

2、调用openpyxl.load_workbook()函数

3、取得Workbook对象

4、调用get_active_sheet()或get_sheet_by_name()工作薄方法

5、取得Worksheet对象

6、使用索引或工作表的cell()方法,带上row和column关键字参数

7、取得Cell对象

8、读取Cell对象的value属性



【扩展】

1、Font对象

Font对象的style属性影响文本在单元格中的显示方式。

要设置字体风格属性,就向Font()函数传入关键字参数。


2、公式

1
2
>>> sheet[ 'B10' ] = '=sum(B1:B9)'
>>> wb.save( 'example.xlsx' )        ##保存


3、调整行和列

Worksheet对象由row_dimensions和column_dimensions属性,控制行高和列宽。

1
2
3
>>> sheet.row_dimensions[ 1 ].height = 70
>>> sheet.column_dimensions[ 'B' ].width = 40
>>> wb.save( 'example.xlsx' )

利用merge_cells()工作表方法,可以将一个矩形区域中的单元格合并为一个单元格。

要拆分单元格,就调用unmerge_cells()工作表方法。

1
2
>>> sheet.merge_cells( 'A1:D3' )
>>> wb.save( 'example.xlsx' )

在OpenPyXL中,每个Worksheet对象都有一个freeze_panes属性,可以设置为一个Cell对象或一个单元格坐标的字符串。

单元格上边的所有行和左边的所有列都会冻结,但单元格所在的行和列不会冻结。

要解冻所有的单元格,就将freeze_panes设置为None或‘A1’。

1
2
>>> sheet.freeze_panes = 'A2'   ##行1将永远可见
>>> wb.save( 'example.xlsx' )

如果需要创建图标,需要做下列事情:

1、从一个矩形区域选择的单元格,创建一个Reference对象

2、通过传入Reference对象,创建一个Series对象

3、创建一个Chart对象

4、将Series对象添加到Chart对象

5、可选地设置Chart对象的drawing.top、drawing.left、drawing.width和drawing.height变量

6、将Chart对象添加到Worksheet对象

如果加载一个Workbook对象,然后马上保存到同样的.xlsx文件名中,实际上会删除其中的图表。







本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/2050824,如需转载请自行联系原作者