python 连库定时生成excel文件并转成可执行文件

import schedule
import pandas as pd
import pymysql, xlwt
from datetime import datetime
from time import strftime
dt = datetime.now()
# print(dt)
def export_excel(dt): # 此处自行修改
# 连接数据库,查询数据
host, port, user, passwd, db = '222.222.204.82', 30202, "root", 'DcP@ssw0rd123', 'share_behavior_log' # 此处自行修改
conn = pymysql.connect(user=user, host=host, port=port, passwd=passwd, db=db, charset='utf8')
cur = conn.cursor()
dt1 = dt.strftime("%Y-%m-%d")
sql = 'select account,action_time,owner_dept_name from user_behavior_log where action_time like CONCAT("%","'+ dt1 +'","%") GROUP BY owner_dept_name' # 此处自行修改
# print(sql)
cur.execute(sql) # 返回受影响的行数

fields = [field[0] for field in cur.description] # 获取所有字段名
all_data = cur.fetchall() # 所有数据
# 写入excel
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')

for col, field in enumerate(fields):
sheet.write(0, col, field)

row = 1
for data in all_data:
for col, field in enumerate(data):
# if col == 4:
# sheet.write(row, col, field)
field=str(field)
sheet.write(row, col, field)
row += 1
dt=dt.strftime("%Y-%m-%d")
book.save("每日在线单位统计表%s.xls" % dt ) # 此处自行修改
# return book
export_excel(dt)
dt = dt.strftime("%Y-%m-%d")
book = r"每日在线单位统计表%s.xls" % dt
df = pd.read_excel(book)
df.rename(columns={"account": "登录帐号", "action_time": "登录时间", "owner_dept_name": "单位"}, inplace=True)
df.to_excel(book, index=False)
posted @ 2022-10-21 13:48  范李2008  阅读(84)  评论(0编辑  收藏  举报