今天研究了一下Python对Excel文件的读取,在此分享一下。
1.模块安装
想通过python对Excel文件进行处理,需要安装以下几个模块:
- pip install xlrd
- pip install xlwt
- pip install xlutils
- pip install pyExcelerator
2.Excel文件读取方法
import xlrd
workbook = xlrd.open_workbook('test.xlsx')
worksheets = workbook.sheet_names()
print(worksheets)
worksheet1 = workbook.sheet_by_name('Sheet1')print(worksheet1)
worksheet2 = workbook.sheets()[0]print(worksheet2)
for worksheet_name in worksheets:worksheet = workbook.sheet_by_name(worksheet_name)print(worksheet)
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))
num_cols = worksheet1.ncols
for curr_clo in range(num_cols):clo = worksheet1.col_values(curr_clo)print('clo %s is: %s' %(curr_clo,clo))
for rown in range(num_rows):for coln in range(num_cols):cell = worksheet1.cell_value(rown,coln)print(cell)
for coln in range(num_cols):for rown in range(num_rows):cell = worksheet1.cell_value(rown,coln)print(cell)
for rown in range(num_rows):for coln in range(num_cols):cell = worksheet1.cell(rown,coln).valueprint(cell)
for rown in range(num_rows):for coln in range(num_cols):cell = worksheet1.row(rown)[coln].valueprint(cell)
for coln in range(num_cols):for rown in range(num_rows):cell = worksheet1.col(coln)[rown].valueprint(cell)
3.案例一
读取Excel文件表单某一列的数据并保存至txt文件
'''
需求:
读取Excel文件 test.xlsx表单Sheet1里第二列数据,并写入到以Sheet1名字命名的.txt文件中
'''
import xlrd
workbook = xlrd.open_workbook('test.xlsx')
worksheets = workbook.sheet_names()
print(worksheets)
worksheet = workbook.sheet_by_name('Sheet1')
print(worksheet)col = worksheet.col_values(1)
print(col)length = len(col)
print(length)with open(worksheets[0]+".txt",'w') as file_obj:for num in range(length):file_obj.write(col[num])file_obj.write('\n')num+=1
4.案例二
读取Excel文件所有表单所有数据并保存至以表单名字命名的txt文件:
'''
需求:
读取Excel文件 test.xlsx所有表单里的所有数据,并写入到以表单名字命名的.txt文件中
'''
import xlrd
workbook = xlrd.open_workbook('test.xlsx')
worksheets = workbook.sheet_names()
print(worksheets)
for worksheet_name in worksheets:worksheet = workbook.sheet_by_name(worksheet_name)print(worksheet)
num_rows = worksheet.nrowsnum_cols = worksheet.ncols
with open(worksheet_name+".txt",'w') as file_obj:for rown in range(num_rows):for coln in range(num_cols):cell = worksheet.cell_value(rown,coln)file_obj.write(str(cell))file_obj.write('\t')file_obj.write('\n')