HappyAndy_Lo 发表于 2022-4-10 00:17 
贴完整代码看看?
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 28 17:05:41 2021
@author: win10
"""
import pymysql
import datetime
from datetime import date
# from openpyxl import load_workbook
# from openpyxl.utils import get_column_letter
import copy
import pandas as pd
# import xlrd
# import copy
import psutil
import os
import xlrd, xlwt
from xlutils.copy import copy as xl_copy
from win32com.client import Dispatch
from win32com.client import DispatchEx
import win32com
import win32com.client as win32
road = 'C:\\Users\\win10\\Desktop\\报表\\'
time =(datetime.datetime.now()+ datetime.timedelta(days=0)).strftime('%Y-%m-%d')
date =(datetime.datetime.now()+ datetime.timedelta(days=0)).strftime('%m%d')
# 建立连接
conn = pymysql.connect(
host = "***",
port = 3306,
user = "***",
passwd = "***",
db = "***",
charset = "utf8"
)
# 建立游标
cursor = conn.cursor()
cursor.execute("CALL NGN_CASE_SUM();") #执行查询语句
res1 = cursor.fetchall()
remind_s=pd.read_sql("SELECT COUNT(*) RD_GP_S FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s=remind_s['RD_GP_S'].tolist()
remind_s_zb=pd.read_sql("SELECT COUNT(DISTINCT POSITION_ID) RD_GP_S_ZB FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s_zz=pd.read_sql("SELECT DISTINCT(SUBSTRING_INDEX(SUBSTRING_INDEX(POSITION_NAME,'_',-1),'(',1)) RD_GP_S_ZZ FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s_jd=pd.read_sql("SELECT COUNT(DISTINCT TASK_TYPE_ID) RD_GP_S_JD FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
# 转换为数字
remind_s_zb=remind_s_zb['RD_GP_S_ZB'].tolist()
remind_s_zz=remind_s_zz['RD_GP_S_ZZ'].tolist()
remind_s_jd=remind_s_jd['RD_GP_S_JD'].tolist()
i=0
sd1=''
sd2=''
for i in range(0,remind_s_zb[0]):
locals()["remind_s"+str(i+1)] = remind_s_zz
#生成动态变量 "qq"+str(a)
locals()["sql_s"+str(i+1)]="SELECT * FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' AND SUBSTRING_INDEX(SUBSTRING_INDEX(POSITION_NAME,'_',-1),'(',1)='"+remind_s_zz+"' ORDER BY CURR_TASK_COUNT DESC"
cursor = conn.cursor()
cursor.execute(locals()["sql_s"+str(i+1)]) #执行查询语句
locals()["remind_s_res"+str(i+1)] = cursor.fetchall()
if i==remind_s_zb[0]-1:
sd1+="remind_s"+str(i+1)+','
sd2+="remind_s_res"+str(i+1)+','
else:
sd1+="remind_s"+str(i+1)+','
sd2+="remind_s_res"+str(i+1)+','
i+=1
conn.commit()
conn.close()
def get_sel_excel(file_excel):
w_excel()
#操作excel
def w_excel():
# open existing workbook
rb = xlrd.open_workbook(road+'数据.xls', formatting_info=True)
# make a copy of it
wb = xl_copy(rb)
title1 = ['当日数据表']
sheet.write(0,0,xlwt.Formula('NOW()'),style1)
sheet.write(1,0,title2,style0)
sheet.write_merge(0,1,1,7,title1,style0)
# 表头
i=1
for header in title3:
if i<=8:
sheet.write(2,i-1,header,style0)
else:
sheet.write(2,i-1,header,style2)
i+=1
if remind_s_jd[0]:
# 第一列
sheet.write_merge(3,remind_s[0]+remind_s_zb[0]+3,0,0,title4[0],style0)
# 最后一行总计
sheet.write_merge(remind_s[0]+remind_s_zb[0]+3,remind_s[0]+remind_s_zb[0]+3,1,3,'总计',style0)
# 组长
i=0
t=0
for i in range(0,remind_s_zb[0]):
if i==0:
sheet.write_merge(3,len(locals()["remind_s_res"+str(i+1)])+3,1,1,locals()["remind_s"+str(i+1)],style0)
else:
sheet.write_merge(t+3,t+len(locals()["remind_s_res"+str(i+1)])+3,1,1,locals()["remind_s"+str(i+1)],style0)
t+=len(locals()["remind_s_res"+str(i+1)])+1
i+=1
# 各组数据
i=0
ct=0
t4=''
t5=''
t6=''
# 写入各组数据
for i in range(0,remind_s_zb[0]):
for row in range(0,len(locals()["remind_s_res"+str(i+1)])):
for col in range(0,len(locals()["remind_s_res"+str(i+1)][row])):
if col==3 or col==4:
if i==0:
sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style7)
else:
sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style7)
else:
if i==0:
sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style6)
else:
sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style6)
row+=1
col+=1
# 各组总计
if i==0:
sheet.write_merge(len(locals()["remind_s_res"+str(i+1)])+3,len(locals()["remind_s_res"+str(i+1)])+3,2,3,'总计',style0)
sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 4,xlwt.Formula('SUM(E4:E'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 5,xlwt.Formula('SUM(F4:F'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 6,xlwt.Formula('SUM(G4:G'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
else:
sheet.write_merge(ct+len(locals()["remind_s_res"+str(i+1)])+3,ct+len(locals()["remind_s_res"+str(i+1)])+3,2,3,'总计',style0)
sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,4,xlwt.Formula('SUM(E'+str(ct+3+1)+':E'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,5,xlwt.Formula('SUM(F'+str(ct+3+1)+':F'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,6,xlwt.Formula('SUM(G'+str(ct+3+1)+':G'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
ct+=len(locals()["remind_s_res"+str(i+1)])+1
if i==0:
t4+='E'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
t5+='F'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
t6+='G'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
else:
t4+=',E'+str(ct+3)
t5+=',F'+str(ct+3)
t6+=',G'+str(ct+3)
if i==remind_s_zb[0]-1:
sheet.write(ct+3,4,xlwt.Formula('SUM('+t4+')'),style0)
sheet.write(ct+3,5,xlwt.Formula('SUM('+t5+')'),style0)
sheet.write(ct+3,6,xlwt.Formula('SUM('+t6+')'),style0)
i+=1
wb.save(road+'数据'+'.xls')
print("导出成功!")
if __name__ == "__main__":
file_excel = road+'数据'+'.xls'
get_sel_excel(file_excel)