用python來自動生成excel數據文件。python處理excel文件主要是第三方模塊庫xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模塊。
方法一:
小羅問我怎么從excel中讀取數據,然后我百了一番,做下記錄
excel數據圖(小羅說數據要給客戶保密,我隨手寫了幾行數據):
python讀取excel文件代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/env python # -*- coding: utf-8 -*- # 讀取excel數據 # 小羅的需求,取第二行以下的數據,然后取每行前13列的數據 import xlrd data = xlrd.open_workbook( 'test.xls' ) # 打開xls文件 table = data.sheets()[ 0 ] # 打開第一張表 nrows = table.nrows # 獲取表的行數 for i in range (nrows): # 循環逐行打印 if i = = 0 : # 跳過第一行 continue print table.row_values(i)[: 13 ] # 取前十三列 |
excel的寫操作等后面用到的時候在做記錄
方法二:
使用xlrd讀取文件,使用xlwt生成Excel文件(可以控制Excel中單元格的格式)。但是用xlrd讀取excel是不能對其進行操作的;而xlwt生成excel文件是不能在已有的excel文件基礎上進行修改的,如需要修改文件就要使用xluntils模塊。pyExcelerator模塊與xlwt類似,也可以用來生成excel文件。
1. [代碼]test_xlrd.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
|
#coding=utf-8 ####################################################### #filename:test_xlrd.py #author:defias #date:xxxx-xx-xx #function:讀excel文件中的數據 ####################################################### import xlrd #打開一個workbook workbook = xlrd.open_workbook( 'E:\\Code\\Python\\testdata.xls' ) #抓取所有sheet頁的名稱 worksheets = workbook.sheet_names() print ( 'worksheets is %s' % worksheets) #定位到sheet1 worksheet1 = workbook.sheet_by_name(u 'Sheet1' ) """ #通過索引順序獲取 worksheet1 = workbook.sheets()[0] #或 worksheet1 = workbook.sheet_by_index(0) """ """ #遍歷所有sheet對象 for worksheet_name in worksheets: worksheet = workbook.sheet_by_name(worksheet_name) """ #遍歷sheet1中所有行row num_rows = worksheet1.nrows for curr_row in range (num_rows): row = worksheet1.row_values(curr_row) print ( 'row%s is %s' % (curr_row,row)) #遍歷sheet1中所有列col num_cols = worksheet1.ncols for curr_col in range (num_cols): col = worksheet1.col_values(curr_col) print ( 'col%s is %s' % (curr_col,col)) #遍歷sheet1中所有單元格cell for rown in range (num_rows): for coln in range (num_cols): cell = worksheet1.cell_value(rown,coln) print cell """ #其他寫法: cell = worksheet1.cell(rown,coln).value print cell #或 cell = worksheet1.row(rown)[coln].value print cell #或 cell = worksheet1.col(coln)[rown].value print cell #獲取單元格中值的類型,類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error cell_type = worksheet1.cell_type(rown,coln) print cell_type """ |
2. [代碼]test_xlwt.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
|
#coding=utf-8 ####################################################### #filename:test_xlwt.py #author:defias #date:xxxx-xx-xx #function:新建excel文件并寫入數據 ####################################################### import xlwt #創建workbook和sheet對象 workbook = xlwt.Workbook() #注意Workbook的開頭W要大寫 sheet1 = workbook.add_sheet( 'sheet1' ,cell_overwrite_ok = True ) sheet2 = workbook.add_sheet( 'sheet2' ,cell_overwrite_ok = True ) #向sheet頁中寫入數據 sheet1.write( 0 , 0 , 'this should overwrite1' ) sheet1.write( 0 , 1 , 'aaaaaaaaaaaa' ) sheet2.write( 0 , 0 , 'this should overwrite2' ) sheet2.write( 1 , 2 , 'bbbbbbbbbbbbb' ) """ #-----------使用樣式----------------------------------- #初始化樣式 style = xlwt.XFStyle() #為樣式創建字體 font = xlwt.Font() font.name = 'Times New Roman' font.bold = True #設置樣式的字體 style.font = font #使用樣式 sheet.write(0,1,'some bold Times text',style) """ #保存該excel文件,有同名文件時直接覆蓋 workbook.save( 'E:\\Code\\Python\\test2.xls' ) print '創建excel文件完成!' |
3. [代碼]test_xlutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#coding=utf-8 ####################################################### #filename:test_xlutils.py #author:defias #date:xxxx-xx-xx #function:向excel文件中寫入數據 ####################################################### import xlrd import xlutils.copy #打開一個workbook rb = xlrd.open_workbook( 'E:\\Code\\Python\\test1.xls' ) wb = xlutils.copy.copy(rb) #獲取sheet對象,通過sheet_by_index()獲取的sheet對象沒有write()方法 ws = wb.get_sheet( 0 ) #寫入數據 ws.write( 1 , 1 , 'changed!' ) #添加sheet頁 wb.add_sheet( 'sheetnnn2' ,cell_overwrite_ok = True ) #利用保存時同名覆蓋達到修改excel文件的目的,注意未被修改的內容保持不變 wb.save( 'E:\\Code\\Python\\test1.xls' ) |
4. [代碼]test_pyExcelerator_read.py
1
2
3
4
5
6
7
8
9
10
11
12
|
#coding=utf-8 ####################################################### #filename:test_pyExcelerator_read.py #author:defias #date:xxxx-xx-xx #function:讀excel文件中的數據 ####################################################### import pyExcelerator #parse_xls返回一個列表,每項都是一個sheet頁的數據。 #每項是一個二元組(表名,單元格數據)。其中單元格數據為一個字典,鍵值就是單元格的索引(i,j)。如果某個單元格無數據,那么就不存在這個值 sheets = pyExcelerator.parse_xls( 'E:\\Code\\Python\\testdata.xls' ) print sheets |
5. [代碼]test_pyExcelerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#coding=utf-8 ####################################################### #filename:test_pyExcelerator.py #author:defias #date:xxxx-xx-xx #function:新建excel文件并寫入數據 ####################################################### import pyExcelerator #創建workbook和sheet對象 wb = pyExcelerator.Workbook() ws = wb.add_sheet(u '第一頁' ) #設置樣式 myfont = pyExcelerator.Font() myfont.name = u 'Times New Roman' myfont.bold = True mystyle = pyExcelerator.XFStyle() mystyle.font = myfont #寫入數據,使用樣式 ws.write( 0 , 0 ,u 'ni hao 帕索!' ,mystyle) #保存該excel文件,有同名文件時直接覆蓋 wb.save( 'E:\\Code\\Python\\mini.xls' ) print '創建excel文件完成!' |