python读取excel表格内不同类型的数据
本文转载自:https://www.cnblogs.com/feiyueNotes/p/7786739.html
本人只是为了方便记录查看,进行转载的;需要查看详细文章可查看原链接。
不同类型数据对应值:
#coding=utf-8
import os
import xlrd
from datetime import datetime,date
newparh = os.chdir(r"D:\Pycharm\Test\test_case\excel")
filename = u'测试数据.xlsx'
file = os.path.join(os.getcwd(),filename)
xl = xlrd.open_workbook(file) #打开文件
table1 = xl.sheet_by_name(u"目录")
def read_excel(table,row,col):
name = table.cell_value(row,col)
type = table.cell_type(row,col)
if type == 0: #空0
name = "''"
elif type == 1: #字符串1
name = name
elif type == 2 and name%1==0: #数字2
name = int(name)
elif type == 3: #日期3
date_value = xlrd.xldate_as_tuple(name,0)
name = datetime(*date_value).strftime('%Y/%m/%d %H:%M:%S')
elif type == 4: #布尔4
name = True if name == 1 else False
elif type == 5: #error 5
name = error
return name
print(read_excel(table1,0,7))