import MySQLdb
import xlrd
import xlwt
from datetime import date,datetime
database = MySQLdb.connect (host="114.116.2.203", user = "root", passwd = "456feixianG", db = "fangniujie",charset="utf8")
# database = MySQLdb.connect (host="127.0.0.1", user = "root", passwd = "", db = "fangniujie",charset="utf8")
cursor = database.cursor()
workbook = xlrd.open_workbook(r'本月TaokeDetail-2018-10-17.xls')
sheet= workbook.sheet_by_name('Page1')
#coding=utf-8
query = "INSERT INTO alimama_order (create_time,click_time,product_info,product_id,shopkeeper_ww,shop,product_num,product_price,order_state,service_rate,payment,forecast_payment,settlement_time,settlement_money,forecast_settlement,order_num,activity_id,insert_time) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s)"
def setnull(date):
if date=="":
return None
else:
return date
datelist=[]
for r in range(1,sheet.nrows):
创建时间=setnull(str(sheet.cell(r,0).value).format('%Y-%m-%d %H:%M:%S'))
#setnull 把时间设置为null 而不是00:00:00 00:00:00这种格式 |
|
# date = xlrd.xldate_as_tuple(sheet.cell(r,1).value,0)
# 点击时间=str(datetime.datetime(*date))
点击时间=setnull(str(sheet.cell(r,1).value).format('%Y-%m-%d %H:%M:%S'))
商品信息=sheet.cell(r,2).value.strip()
商品ID=sheet.cell(r,3).value.strip()
掌柜旺旺=sheet.cell(r,4).value.strip()
所属店铺=sheet.cell(r,5).value.strip()
商品数=str(sheet.cell(r,6).value)
商品单价=str(sheet.cell(r,7).value)
订单状态=sheet.cell(r,8).value.strip()
服务费率=(sheet.cell(r,9).value.strip()).replace(' ','')
付款金额=str(sheet.cell(r,10).value)
预估付款服务费=str(sheet.cell(r,11).value)
if sheet.cell(r,12).ctype == 1:
结算时间=setnull(sheet.cell(r,12).value.strip())
else:
结算时间=str(sheet.cell(r,12).value).format('%Y-%m-%d %H:%M:%S')
#Python读取Excel中单元格的内容返回的有5种类型,即上面例子中的ctype:
#ctype : 0 empty,1 string,2 number, 3 date,4 boolean,5 error
结算金额=str(sheet.cell(r,13).value)
预估结算服务费=str(sheet.cell(r,14).value).strip()
订单编号=sheet.cell(r,15).value.strip()
活动id=sheet.cell(r,16).value.strip()
date=datetime.now()
插入时间=date.strftime('%Y-%m-%d %H:%M:%S')
values = (创建时间,点击时间,商品信息,商品ID,掌柜旺旺,所属店铺,商品数,商品单价,订单状态,服务费率,付款金额,预估付款服务费,结算时间,结算金额,预估结算服务费,
订单编号,活动id,插入时间)
datelist.append(values)
if(r%1000==0 or r==(sheet.nrows-1)):
try:
cursor.executemany(query, datelist)
database.commit()
datelist.clear()
except Exception as err:
print("Error:"+err)
cursor.close()
database.close()