diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000000000000000000000000000000000000..2a243f8fa803109f9c0aa4ba894861990bec38e8 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql_aurora.aws_wrapper + true + software.amazon.jdbc.Driver + jdbc:aws-wrapper:mysql://localhost:3306/pytest + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/mysqlandxlsx/exceltest.py b/mysqlandxlsx/exceltest.py new file mode 100644 index 0000000000000000000000000000000000000000..8deb413190f14f2545722d1ea26dc453e1b82960 --- /dev/null +++ b/mysqlandxlsx/exceltest.py @@ -0,0 +1,131 @@ +from openpyxl import workbook, Workbook,load_workbook + +import datetime + +from openpyxl.chart import BarChart, Reference, PieChart + +#创建 +# wb=Workbook() +# ws=wb.active +# ws['A1']=42 +# ws.append([1,2,3]) +# ws['A2']=datetime.datetime.now() +# wb.save('test.xlsx') + +#打开/操作sheet +# wb=load_workbook('test.xlsx') +# ws=wb.active +# +# ws1=wb.create_sheet('Sheet1') +# ws1.title='新表单' +# ws2=wb.create_sheet('Sheet2',0) +# ws2.title='你好' +# #设置颜色 +# ws1.sheet_properties.tabColor='1072BA' +# #获取sheet +# ws3=wb['你好'] +# #复制sheet +# ws1_copy=wb.copy_worksheet(ws1) +# #删除 +# wb.remove(ws1) +# +# wb.save('test.xlsx') + +#操作单元格 +wb=load_workbook('../test.xlsx') +# ws1=wb['你好'] +# ws1['A1']=123 +# ws1['B2']='你好' + +#批量操作 +#单列 +# for cell in ws1['A']: +# print(cell.value) +# # 操作单行 4 +# for cell in ws1["1"]: +# print(cell.value) +# # 操作多列 7 +# for column in ws1['A:C']: +# for cell in column: +# print(cell.value) +# # 操作多行 11 +# for row in ws1['1:3']: +# for cell in row: +# print(cell.value) +# # 指定范围 15 +# for row in ws1['A1:C3']: +# for cell in row: +# print(cell.value) + +#所有行 +# for row in ws1.iter_rows(): +# for cell in row: +# print(cell.value) +# #所有列 +# for column in ws1.iter_cols(): +# for cell in column: +# print(cell.value) +# # 合并 +# ws1.merge_cells('A2:D2') +# # 解除合并 +# ws1.unmerge_cells('A2:D2') + +#图表 +# ws2=wb['你好1'] +# +# rows=[ +# ('月份', '苹果', '香蕉'), +# (1, 43, 25), +# (2, 10, 30), +# (3, 40, 60), +# (4, 50, 70), +# (5, 20, 10), +# (6, 10, 40), +# (7, 50, 30), +# ] +# +# for row in rows: +# ws2.append(row) + + # 创建柱状图对象 + # chart1 = BarChart() + # chart1.type = 'col' # 图表类型:col 是垂直柱状图(bar 是水平的) + # chart1.style = 10 # 图表样式编号,10 是一种配色风格 + # chart1.title = '销售柱状图' # 图表标题 + # chart1.y_axis.title = '销量' # Y 轴标题 + # chart1.x_axis.title = '月份' # X 轴标题 + # + # # 指定数据区域:第1列到第3列,第1行到第8行 + # data = Reference(ws2, min_col=1, min_row=1, max_row=8, max_col=3) + # # 指定类别标签区域:第1列,第1行到第8行(通常是月份名称) + # series = Reference(ws2, min_col=1, min_row=1, max_row=8) + # + # # 把数据添加到图表,第一行作为图例标题 + # chart1.add_data(data, titles_from_data=True) + # # 把第1列的值设为 X 轴的分类标签(如1月、2月...) + # chart1.set_categories(series) + # + # # 把图表插入到 ws2 的 A10 单元格位置 + # ws2.add_chart(chart1, 'A10') + # + +#圆形图 +data = [ + ['水果', '销量'], + ['苹果', 50], + ['樱桃', 30], + ['橘子', 10], + ['香蕉', 40], +] +ws3=wb['Sheet'] +for row in data: + ws3.append(row) +pie=PieChart() +pie.title='水果销售占比' +labels=Reference(ws3,min_col=1, min_row=2, max_row=5) +data = Reference(ws3, min_col=2, min_row=1, max_row=5) +pie.add_data(data, titles_from_data=True) +ws3.add_chart(pie,'D1') + +wb.save('test.xlsx') + diff --git a/mysqlandxlsx/mysql.py b/mysqlandxlsx/mysql.py new file mode 100644 index 0000000000000000000000000000000000000000..48ff3387a01761fdbb17e05924c365ac4150b880 --- /dev/null +++ b/mysqlandxlsx/mysql.py @@ -0,0 +1,41 @@ +import xlrd +import pymysql +db=pymysql.connect( + host="localhost", + port=3306, + user="root", + database='pytest', + password='abcd13582796800', + charset='utf8' +) +cursor = db.cursor() +wb = xlrd.open_workbook('../员工的人员信息.xls') +ws = wb.sheet_by_name('人员管理') + +data=[] +for i in range(1,ws.nrows): + row=ws.row_values(i) + data.append(( + int(row[0]), # id → INT + str(row[1]), # idcard + str(row[2]), # username + str(row[3]), # realname + str(row[4]), # pwd → VARCHAR,不要转int + str(row[5]), # telphone + str(row[6]), # email + int(row[7]), # age → INT + str(row[8]), # sex + str(row[9]), # address + row[10], # hiredate + float(row[11]), # sal → DOUBLE + str(row[12]), # job + str(row[13]) # company + )) +cursor.executemany(''' +INSERT INTO user_info(`id`,`idcard`,`username`,`realname`,`pwd`,`telphone`, + `email`,`age`,`sex`,`address`,`hiredate`,`sal`,`job`,`company`) + VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) +''',data) +db.commit() +cursor.close() +db.close() diff --git a/mysqlandxlsx/mysqltest.py b/mysqlandxlsx/mysqltest.py new file mode 100644 index 0000000000000000000000000000000000000000..2a149d454c19bb424e665e16a66258ca8e394333 --- /dev/null +++ b/mysqlandxlsx/mysqltest.py @@ -0,0 +1,59 @@ +import time +import pymysql + +db=pymysql.connect( + host="localhost", + port=3306, + user="root", + database='pytest', + password='abcd13582796800', + charset='utf8' +) +#创建游标 +cursor = db.cursor() +#查 +# sql='SELECT * FROM users' +# cursor.execute(sql) +# result = cursor.fetchall() +# for i in result: +# print(i) +# cursor.execute('SELECT * FROM users WHERE age>%s',25) +# result2 = cursor.fetchall() +# for i in result2: +# print(i) + +# #建表 +# sql2=''' +# CREATE TABLE IF NOT EXISTS users ( +# id INT PRIMARY KEY AUTO_INCREMENT, +# name VARCHAR(50) NOT NULL, +# age INT, +# email VARCHAR(100) +# ) +# ''' + +#插入数据 +# user=[ +# ('李四', 30, 'lisi@example.com'), +# ('王五', 28, 'wangwu@example.com'), +# ('赵六', 22, 'zhaoliu@example.com') +# ] +# sql3=f'INSERT INTO users (name, age, email) VALUES (%s, %s, %s)' +# cursor.executemany(sql3,user) +# db.commit() + +#更新 +# cursor.execute( +# "UPDATE users SET name=%s WHERE name=%s", +# ('test','李四') +# ) +# db.commit() + +#删除 +cursor.execute( + 'DELECT FROM users WHERE id=%s' +) + +cursor.close() +db.close() + diff --git a/mysqlandxlsx/xlsx.py b/mysqlandxlsx/xlsx.py new file mode 100644 index 0000000000000000000000000000000000000000..109adca793c82fde0b9413eea9c59f0f40d08c94 --- /dev/null +++ b/mysqlandxlsx/xlsx.py @@ -0,0 +1,101 @@ +from tkinter import image_names + +import xlrd + +wb = xlrd.open_workbook('../员工的人员信息.xls') +ws = wb['人员管理'] +#统计表格中有多少人 +print(f"一共有{ws.nrows - 1}人") +#统计办电信,联通,移动的用户数量并计算出三种用户的占比 +yd=0 +lt=0 +dx=0 +phone1=[] +phone2=[] +# 移动网络 +yd_3 = ['134', '135', '136', '137', '138', '139', '147', + '150', '151', '152', '157', '158', '159', '178', + '182', '183', '184', '187', '188'] +yd_4 = ['1703', '1705', '1706'] + +# 联通网络 +lt_3 = ['130', '131', '132', '145', '155', '156', + '175', '176', '185', '186', '171'] +lt_4 = ['1704', '1707', '1708', '1709'] + +# 电信网络 +dx_3 = ['133', '149', '153', '173', '177', + '180', '181', '189'] +dx_4 = ['1700', '1701', '1702'] +for i in range(1,ws.nrows): + phone=str(int(ws.cell_value(i,5))) + phone1=phone[:3] + phone2=phone[:4] + if phone1 in yd_3: + yd+=1 + elif phone1 in lt_3: + lt+=1 + elif phone1 in dx_3: + dx+=1 + + if phone2 in yd_4: + yd+=1 + elif phone2 in lt_4: + lt+=1 + elif phone2 in dx_4: + dx+=1 + +sum=yd+lt+dx + +print(f"移动占比{yd/sum*100:.2f}%") +print(f"联通占比{lt/sum*100:.2f}%") +print(f"电信占比{dx/sum*100:.2f}%") + +#总公司男女人数 +male=0 +female=0 +for i in range(1,ws.nrows): + ism=ws.cell_value(i,8) + if ism=='男': + male+=1 + else: + female+=1 +print(f"男性有{male}人,女性有{female}人") + +#年龄超过45岁的老员工人数 +num=0 +for i in range(1,ws.nrows): + iam=ws.cell_value(i,7) + if iam>45: + num+=1 +print(f"年龄超过45岁的老员工人数为{num}人") + +#薪资高于8000元的高薪人员数量和薪资低于3000的底薪人员数量 +up=0 +down=0 +for i in range(1,ws.nrows): + iam=ws.cell_value(i,11) + if iam>8000: + up+=1 + elif iam<3000: + down+=1 +print(f"薪资高于8000元的高薪人员数量为{up}薪资低于3000的底薪人员数量为{down}") +#统计去传媒公司的工作的人员数量 +a=0 +for i in range(1,ws.nrows): + iam=ws.cell_value(i,13) + if iam.find('传媒')!=-1: + a+=1 +print(f"去传媒公司的工作的人员数量{a}") + +#统计一下可能在疫情高危地区的人数(高危地区:黑龙江,北京,福建,四川) +b=0 +c=['黑龙江','北京','福建','四川'] +for i in range(1,ws.nrows): + iam=ws.cell_value(i,9) + for j in c: + if j in iam: + b+=1 + break +print(f"高危地区的人数{b}") + diff --git a/test.xlsx b/test.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..4321beaa13e101cb05131ab270ca57a0a2d69e4f Binary files /dev/null and b/test.xlsx differ diff --git "a/\345\221\230\345\267\245\347\232\204\344\272\272\345\221\230\344\277\241\346\201\257.xls" "b/\345\221\230\345\267\245\347\232\204\344\272\272\345\221\230\344\277\241\346\201\257.xls" new file mode 100644 index 0000000000000000000000000000000000000000..b8df7d51ef4329176717db678dcd3895fd67534e Binary files /dev/null and "b/\345\221\230\345\267\245\347\232\204\344\272\272\345\221\230\344\277\241\346\201\257.xls" differ