各位大佬,咨询一个问题:
使用Python 提取数据库中数据 并录入excel 指定也签下的指定单元格。当前,操纵数据库,提取信息,等等步骤都没有问题,但是最后一步出问题了: 数据无法正常写入到excel中(excel中不显示)
如图:
我的代码如下,请求大神指点啊
import pymysql import tkinter import os import openpyxl from tkinter import * import time def newcreated(): ## 用来提取数据 conn = pymysql.connect(host='localhost',user='root',passwd='QAZwsx12345678',db='test',port=3306,charset='utf8') cur = conn.cursor() cur.execute("use test") cur.execute("SELECT SUM(sql_data.`总价`) FROM sql_data WHERE sql_data.`销售区域`= '东大区'") a = cur.fetchall() time.sleep(0.2) cur.execute("SELECT SUM(sql_data.`总价`) FROM sql_data WHERE sql_data.`销售区域`= '西大区'") b = cur.fetchall() time.sleep(0.2) cur.execute("SELECT SUM(sql_data.`总价`) FROM sql_data WHERE sql_data.`销售区域`= '南大区'") c = cur.fetchall() cur.execute("SELECT SUM(sql_data.`总价`) FROM sql_data WHERE sql_data.`销售区域`= '北大区'") d = cur.fetchall() #b = a.__str__() cur.close() conn.close return a,b,c,d ##返回b的值 t = tuple(newcreated()) #print(t) class control : def __init__(self, master): ##用来建造GUI 控制界面 fm1 = Frame(master) Button(fm1, text='生成数据',command = self.exc).pack(side=TOP, anchor=W, fill=X, expand=NO,padx = 10, pady = 10,ipadx=10,ipady = 4) fm1.pack(side=LEFT, fill=BOTH, expand=YES) def exc(self): ##用来提取表格,并对表格进行赋值的 wb = openpyxl.load_workbook('C:/Users/lenovo/Desktop/小小科技/show_6.15/6_15/show_6_15.xlsx') sheet = wb.get_sheet_by_name('页签1') sheet['C4'] = t [0][0][0] sheet['C5'] = t [1][0][0] sheet['C6'] = t [2][0][0] sheet['C7'] = t [3][0][0] print(sheet['C4'].value) print(sheet['C5'].value) print(sheet['C6'].value) print(sheet['C7'].value) wb.save('show_6_15.xlsx') root = Tk() root.title("Control") display = control(root) root.mainloop()
pardon110
相关分类