python中excel制作成绩报表_python制作简单excel统计报表2之操作excel的模块openpyxl简单用法...
python制作簡單excel統計報表2之操作excel的模塊openpyxl簡單用法
# coding=utf-8 from openpyxl import Workbook, load_workbook from openpyxl.drawing.image import Image from openpyxl.styles import Font,colors from datetime import datetime import MySQLdb class ExcelUtils(object): """ pip install openpyxl pip install pillow 參考文檔:https://openpyxl.readthedocs.io/en/stable """ def __init__(self): self.wb = Workbook() # 激活表單 self.ws = self.wb.active self.ws_two = self.wb.create_sheet('我的表單') self.ws.title = '你的表單' self.ws.sheet_properties.tabColor = 'ff000000' self.ws_three = self.wb.create_sheet() def do_sth(self): # 插入數據 self.ws['A1'] = 66 self.ws['A2'] = '你好' self.ws['A3'] = datetime.now() # 批量插入數字 for row in self.ws_two['A1:E5']: for cell in row: cell.value = 2 # 對數據進行求和 self.ws_two['G1'] = '=SUM(A1:E1)' # 插入圖片 # img = Image('./static/temp.jpg') # self.ws.add_image(img,'B1') # 合并單元格 self.ws.merge_cells('A4:E5') self.ws.unmerge_cells('A4:E5') # 插入文字 font = Font(sz=18,color = colors.RED) self.ws['A2'].font = font self.wb.save('./static/test.xlsx') def read_xls(self): """ 讀取excel數據 :return: """ ws = load_workbook('./static/templates.xlsx') names = ws.get_sheet_names() print(names) # 獲取sheet的三種方法 # wb = ws.active # wb = ws['北京大學統計'] wb = ws[names[0]] # 先讀取行 for row in wb.rows: # 再讀取列 for cell in row: print(cell.value) def get_conn(self): """ 獲取mysql的連接""" try: self.conn = MySQLdb.connect( host="localhost", port=3306, user="root", password="root", db="user_grade", ) except MySQLdb.Error as e: print("Error %d:%s" % (e.args[0], e.args[1])) return self.conn def import_excel_todb(self): # 將excel中的內容導入數據庫中 ws = load_workbook('./static/templates.xlsx') names = ws.get_sheet_names() # 獲取數據的sheet wb = ws[names[0]] conn = self.get_conn() for (i,row) in enumerate(wb.rows): # 跳過標題部分 if i < 2: continue # 獲取年份,最高分,平均分 year = wb['A{0}'.format(i+1)].value max = wb['B{0}'.format(i+1)].value avg = wb['C{0}'.format(i+1)].value # print(year, max, avg) cur = conn.cursor() # sql = "insert into score(year,max,avg) values(2005, 695, 468)" sql = "insert into score(year,max,avg) values({year},{max},{avg})".format(year = year,max = max,avg = avg) cur.execute(sql) conn.autocommit(True) conn.close() def export_db_toexcel(self): # 從數據庫導數據到excel里面 # 從數據庫中獲取數據 conn = self.get_conn() cur = conn.cursor() cur.execute("select year,max,avg from score") # 獲取所有數據 rows = cur.fetchall() # for row in rows: # print(row[0],row[1],row[2]) wb = Workbook() ws = wb.active ws.title = "高考成績" for (i,row) in enumerate(rows): # 單個賦值 # ws['A{0}'.format(i+1)] = row[0] # ws['B{0}'.format(i+1)] = row[1] # ws['C{0}'.format(i+1)] = row[2] # 批量賦值 (ws['A{0}'.format(i+1)], ws['B{0}'.format(i + 1)], ws['C{0}'.format(i + 1)] ) = row wb.save("./static/myscore.xlsx") # 關閉數據庫連接 conn.close() if __name__ == "__main__": client = ExcelUtils() # client.do_sth() # client.read_xls() # conn = client.get_conn() # client.import_excel_todb() client.export_db_toexcel()
CREATE TABLE `score` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`year` int(11) DEFAULT NULL,
`max` int(11) DEFAULT NULL,
`avg` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
static/templates.xlsx
插入數據庫的信息:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python中excel制作成绩报表_python制作简单excel统计报表2之操作excel的模块openpyxl简单用法...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信安卓平板专用版(微信 安卓平板)
- 下一篇: python+robotframewor