forked from gxcuizy/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### 文件结构 | ||
|
||
``` | ||
├── excel_openpyxl.py # openpyxl模块处理 | ||
├── excel_pandas.py # pandas模块处理 | ||
``` | ||
|
||
### 功能描述 | ||
|
||
在原表数据上,需要追加新的数据,例如表格数据为: | ||
|
||
``` | ||
1 2 3 4 5 6 | ||
1 2 3 4 5 6 | ||
1 2 3 4 5 6 | ||
1 2 3 4 5 6 | ||
1 2 3 4 5 6 | ||
1 2 3 4 5 6 | ||
``` | ||
|
||
我们想要在第6列后新增2列数据,又要维持原来数据不变,就可以在原表数据上进行增加新数据即可。 | ||
|
||
### 交流学习 | ||
|
||
如有写的不对或者错误的地方,希望大家指正,相互交流,谢谢。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
openpyxl模块处理表格数据,在当前表格行中增加数据并保存到原表格 | ||
author: gxcuizy | ||
time: 2021-03-02 | ||
""" | ||
|
||
import openpyxl | ||
from openpyxl import load_workbook | ||
import pymysql | ||
|
||
|
||
def get_mysql_info(cursor, order_code): | ||
"""获取数据库数据""" | ||
# 组装数据sql | ||
sql = "SELECT order_total, order_status FROM order WHERE order_code = '%s'" % (order_code,) | ||
# 获取一条数据 | ||
cursor.execute(sql) | ||
result = cursor.fetchone() | ||
return result | ||
|
||
|
||
# 程序主入口 | ||
if __name__ == "__main__": | ||
# 创建一个连接 | ||
db_host = '127.0.0.1' | ||
db_name = 'test' | ||
db_user = 'root' | ||
db_pw = 'root' | ||
db_port = 3306 | ||
db = pymysql.connect(host=db_host, user=db_user, password=db_pw, database=db_name, port=db_port) | ||
# 用cursor()创建一个游标对象 | ||
cursor_obj = db.cursor(cursor=pymysql.cursors.DictCursor) | ||
# 读取excel数据 | ||
print('开始读取表格数据……') | ||
file_name = '1.xlsx' | ||
wb = load_workbook(file_name, data_only=True) | ||
sheet = wb.active | ||
# 写表excel数据 | ||
print('读取数据完毕,开始处理数据……') | ||
write_wb = openpyxl.Workbook() | ||
write_sheet = write_wb.active | ||
# 循环处理Excel数据(假设保留原先的1-3列数据,在第4列和第5列增加订单金额和状态) | ||
for row in sheet.rows: | ||
# 保留原来行数据 | ||
row_1 = row[0].value | ||
row_2 = row[1].value | ||
row_3 = row[2].value | ||
# 获取附加的行数据 | ||
print('正在处理order_code=%s' % row_1) | ||
info = get_mysql_info(cursor_obj, row_1) | ||
row_4 = '' | ||
row_5 = '' | ||
if info: | ||
row_4 = info['order_total'] | ||
val_5 = info['order_status'] | ||
excel_row = [ | ||
row_1, | ||
row_2, | ||
row_3, | ||
row_4, | ||
row_5, | ||
] | ||
# 写入Excel中 | ||
write_sheet.append(excel_row) | ||
# 生成excel文件 | ||
write_wb.save(file_name) | ||
print('写入excel完成!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
pandas处理表格数据,在当前表格行中增加数据并保存到原表格 | ||
author: gxcuizy | ||
time: 2021-03-02 | ||
""" | ||
|
||
import pandas | ||
from pandas import DataFrame | ||
import pymysql | ||
|
||
|
||
def get_mysql_info(cursor, order_code): | ||
"""获取数据库数据(根据自己的实际情况来修改)""" | ||
# 组装数据sql | ||
sql = "SELECT order_total, order_status FROM order WHERE order_code = '%s'" % (order_code,) | ||
# 获取一条数据 | ||
cursor.execute(sql) | ||
result = cursor.fetchone() | ||
return result | ||
|
||
|
||
# 程序主入口 | ||
if __name__ == "__main__": | ||
# 创建一个连接 | ||
db_host = '127.0.0.1' | ||
db_name = 'test' | ||
db_user = 'root' | ||
db_pw = 'root' | ||
db_port = 3306 | ||
db = pymysql.connect(host=db_host, user=db_user, password=db_pw, database=db_name, port=db_port) | ||
# 用cursor()创建一个游标对象 | ||
cursor_obj = db.cursor(cursor=pymysql.cursors.DictCursor) | ||
# 读取excel数据 | ||
print('开始读取表格数据……') | ||
file_name = '1.xlsx' | ||
sheet = pandas.read_excel(file_name) | ||
# 数据转列表 | ||
data = sheet.values.tolist() | ||
# 循环处理数据 | ||
print('读取数据完毕,开始处理数据……') | ||
for i, val in enumerate(data): | ||
# 获取附加的行数据 | ||
print('正在处理order_code=%s' % val[0]) | ||
info = get_mysql_info(cursor_obj, val[0]) | ||
if info: | ||
data[i].append(info['order_total']) | ||
data[i].append(info['order_status']) | ||
# 表头信息,转数据格式存储 | ||
excel_header = ['sort', 'name', 'stu_no', 'course', 'score'] | ||
data = pandas.DataFrame(data, columns=excel_header) | ||
data.to_excel(file_name, index=False, header=True) | ||
print('写入excel完成!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters