首页 > 编程语言 >python 连库定时生成excel文件并转成可执行文件

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

时间:2022-10-21 13:55:54浏览次数:52  
标签:-% python 连库 excel field book dt col

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)

标签:-%,python,连库,excel,field,book,dt,col
From: https://www.cnblogs.com/funny2008/p/16813209.html

相关文章

  • Python打包成exe,包含cx_oracle的情况
    当Python程序应用了cx_oracle包,打包成功之后,exe文件不能正常运行。需要按照以下步骤执行1,执行pyinstaller-Fmain.py,执行之后生成一个main.spec文件2,按照下图修改spec文......
  • 为什么 Python 会如此火爆?
    python是编程语言,属于高级语言,python之所以火爆,主要是爬虫的应用,也就是你可以来做自己的大数据,用python来进行搜索数据!第二python应用在人工智能上,效果较好!第三:它属于胶水......
  • Python | 用Python制作送给女票的生日礼物
    HappyBirthday程序视频地址:​​https://www.bilibili.com/video/BV1R7411C7A1​​代码地址:​​https://github.com/borninfreedom/HappyBirthday​​截图: ......
  • 爬取起点小说信息存入excel
    点击查看代码importurllib.requestfromlxmlimportetreeimportxlwt#请求地址url='https://www.qidian.com/all/action1-page1'#用户代理headers={......
  • spring导入excel
    springboot导入excel引入pom依赖<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.13</version></dependency>......
  • python3 quick_ratio计算结果不稳定的解决方式
    网上找不到,也许是大家都不用,个人遇到了,所以记录下解决方案seq=difflib.SequenceMatcher(None)seq是一个类,计算结果会存起来当使用单例模式时,上一次计算结果会影响本......
  • java版Excel文件导入数据库源代码
       java版Excel文件导入数据库源代码  servlet容器:tomcat(或者其他)数据库:oracle(或者其他)使用框架:jsp+struts1.2支持字符型/数据型/日期型/CLOB等数据使用步骤:1.......
  • Python2和Python3的区别
    1.性能Py3.0运行pystonebenchmark的速度比Py2.5慢30%。Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可以取得很好的优化结果。Py3.1性能比Py2.5慢15%,还有很大......
  • python 爬取网页,带有post参数
    https://blog.csdn.net/weixin_40340586/article/details/119457955 记录一下自己的爬虫踩过的坑,上一次倒是写了一些,但是写得不够清楚,这次,写清楚爬取的过程。这个网站......
  • 【http代理】Python-Selenium-Chrome(白名单使用)代码样例
    1.基于白名单方式使用Selenium+Chrome认证代理2.运行环境要求python2/3+selenium+Chrome+Chromedriver+Windows/Linux/macOS03.下载chromedriver(注意chromedrive......