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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import xlrd import pymysql import datetime def main(): db=pymysql.connect(host='localhost',port=3306,user='root',password='root',db='myself',charset='utf8') cursor=db.cursor() filename='./express.xlsx' try: data=xlrd.open_workbook(filename) except Exception as e: print('open excel file failed!') raise e query='''insert into express(name,year,src_province,dst_province,first_weight,other_price,remark,modify_time) values(%s,%s,%s,%s,%s,%s,%s,%s)''' name='' year='' src_province='' dst_province='' first_weight='' other_price='' remark='' sheetnames=data.sheet_names(); for i in range(len(sheetnames)): table=data.sheet_by_index(i) src_province = '吉林' year='2020' nameList=['德邦','顺丰','邮政','申通','京东'] provinceList=['','','','',''] firstList=['','','','',''] otherList=['','','','',''] remarkList=['','','','',''] remarkList[0]=table.cell(table.nrows-1,0).value remarkList[1]=table.cell(table.nrows-2,4).value remarkList[2]=table.cell(table.nrows-1,8).value remarkList[3]=table.cell(table.nrows-2,12).value remarkList[4]=table.cell(table.nrows-2,16).value for j in range(3,table.nrows-2): provinceList[0]=table.cell(j,0).value provinceList[1]=table.cell(j,4).value provinceList[2]=table.cell(j,8).value provinceList[3]=table.cell(j,12).value provinceList[4]=table.cell(j,16).value firstList[0]=table.cell(j,1).value firstList[1]=table.cell(j,5).value firstList[2]=table.cell(j,6).value firstList[3]=table.cell(j,13).value firstList[4]=table.cell(j,17).value otherList[0]=table.cell(j,2).value otherList[1]=table.cell(j,6).value otherList[2]=table.cell(j,10).value otherList[3]=table.cell(j,14).value otherList[4]=table.cell(j,18).value for k in range(5): name=nameList[k] dst_province=provinceList[k] first_weight=firstList[k] other_price=otherList[k] remark=remarkList[k] modify_time=datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') values=(name,year,src_province,dst_province,first_weight,other_price,remark,modify_time) try: cursor.execute(query,values) except Exception as e: print(e)
cursor.close() db.commit() db.close()
if __name__ == '__main__': main()
|