需求
- 读取mysql数据,使用SQL过滤数据
- SQL结果追加数据到EXCEL 文件
"""
Created on 2021/12/29 16:44
@author: tange
"""
from sqlalchemy import create_engine
import pandas as pd
import time
curr_date = time.strftime("%Y%m%d", time.localtime())
print(curr_date)
res_path = "D:/code_fileAll/test_file/output/"
engine = create_engine("mysql+pymysql://root:root@10.1.60.110:3306/datax?charset=utf8mb4",
max_overflow=0,
pool_size=5,
pool_timeout=30,
pool_recycle=-1,
encoding='utf-8',
echo=False
)
OriginSql = """
select * from datax.datax_fdl ;
"""
allData = engine.execute(OriginSql)
res_file_name = f"mysql_{curr_date}.xlsx"
df = pd.DataFrame(allData, columns=['id', 'lable', 'lable2', 'lable3', 'lable4', 'create_time', 'update_time'])
df.to_excel(res_path + res_file_name
, index=False
, sheet_name='mysql'
, na_rep=0
, header=None
)
df2 = df.copy()
with pd.ExcelWriter(res_path + res_file_name, mode='a', engine="openpyxl") as writer:
df2.to_excel(writer, sheet_name='mysql', index=False)
df2.to_excel(writer, sheet_name='mysql3', index=False)
writer.close()
|