Python Execl导入
import xlrd
from xlrd import open_workbook
def getExeclInfo(filepath,colnameindex=0,by_index=0): data = xlrd.open_workbook(filepath) table = data.sheets()[by_index] # 行数 nrows = table.nrows # 列数 ncols = table.ncols colnames = table.row_values(colnameindex) list = [] for rownum in range(1,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return list
数据插入 def insertExeclInfo(list): session = db.session for dict in list: if list.index(dict) > 0: print(dict) try: demo = models.Demo(id=dict['工号'],entrydate=dict['入职日期'],entryreasons=dict['入职原因'],RSscope=dict['人事范围']) demotwo = models.DemoTwo(id = dict['工号'],surname = dict['姓'],name = dict['名'],woman = dict['女'],man = dict['男'],birthdata = dict['出生日期'],birthcountry = dict['国籍'],maritalstatus = dict['婚姻状态']) session.add(demotwo) session.add(demo) except Exception as e: return """<html><body><h1>数据插入异常</h1><p>Your chard could not be charged. Please check the number and/or contact your credit card company.</p></body></html>""" session.commit()