记录下python读取excel的做法。

python读取excel可以用xlrd这个库,使用起来还算简单。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import xlrd
from datetime import date

filename = 'MIL_update.xlsx'
with xlrd.open_workbook(filename) as excel_file:
    sheet = excel_file.sheets()[0]  # 取得第一个sheet,还可以用sheet_by_index方法拿到指定sheet
    for line in sheet._cell_values[1:]: 
        period_name = int(line[1])
        pay_day = date(*xlrd.xldate_as_tuple(line[2], excel_file.datemode)[:3]).strftime('%Y-%m-%d')
        check_payroll_day = date(*xlrd.xldate_as_tuple(line[3], excel_file.datemode)[:3]).strftime('%Y-%m-%d')
        data.append((period_name, pay_day, check_payroll_day))

这里xlrd.open_workbook返回一个excel对象,除了直接用sheets()获取所有sheet还可以用sheet_by_index拿到指定index的sheet。excel的内容存放在sheet的_cell_values这个二维数组变量中,可以通过sheet.cell_value(rowx, colx)通过两个纬度的index来访问,

值得注意的一点是,excel中关于日期格式的处理,如果一个单元格是日期格式,那么这里读到的很有可能是一个float类型的数字,而不是excel展示的日期,需要调用xlrd.xldate_as_tuple(value, date_mode)来转换一下,xldate_as_tuple有两个参数,第一个是需要转换的值,第二个是转换模式,可以直接用excel对象的datemode。