Skip to content

Commit

Permalink
处理Excel表格数据
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcuizy committed Mar 2, 2021
1 parent b039274 commit 947f484
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 8 deletions.
25 changes: 25 additions & 0 deletions 处理Excel数据原表保存/README.md
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列数据,又要维持原来数据不变,就可以在原表数据上进行增加新数据即可。

### 交流学习

如有写的不对或者错误的地方,希望大家指正,相互交流,谢谢。
70 changes: 70 additions & 0 deletions 处理Excel数据原表保存/excel_openpyxl.py
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完成!')
55 changes: 55 additions & 0 deletions 处理Excel数据原表保存/excel_pandas.py
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完成!')
16 changes: 8 additions & 8 deletions 拼接表格单行数据为字符串/join_excel_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time


def echo_msg(msg=''):
def print_msg(msg=''):
"""打印信息"""
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('[' + now_time + '] ' + msg)
Expand All @@ -26,34 +26,34 @@ def echo_msg(msg=''):
line_num = input('请输入要拼装的数据第几列(例如“1”):')
# 判断文件是否存在
if os.path.exists(file_name) == False:
echo_msg('文件不存在')
print_msg('文件不存在')
os.system("pause")
exit(0)
# 判断输入的行数是否为数字
if line_num.isdigit() == False:
echo_msg('请输入列数的数字')
print_msg('请输入列数的数字')
os.system("pause")
exit(0)
try:
# 获取表格数据
echo_msg('开始获取文件[' + file_name + ']的第[' + str(line_num) + ']列数据')
print_msg('开始获取文件[' + file_name + ']的第[' + str(line_num) + ']列数据')
line_num = int(line_num) - 1
sheet = pandas.read_excel(io=file_name, usecols=[line_num])
data = sheet.values.tolist()
str_data = ''
# 循环处理数据
echo_msg('已获取列数据条数[' + str(len(data)) + '],开始处理数据……')
print_msg('已获取列数据条数[' + str(len(data)) + '],开始处理数据……')
for x in range(len(data)):
if str(data[x][0]) != 'nan':
str_data += "'" + str(data[x][0]) + "',"
# 写入文本文件
echo_msg('数据处理完毕,开始写入……')
print_msg('数据处理完毕,开始写入……')
random_num = random.randint(1000, 9999)
with open('str_' + str(random_num) + '.txt', 'w') as f:
f.write(str_data.strip(','))
echo_msg('数据写入完毕.')
print_msg('数据写入完毕.')
except Exception as err_info:
# 异常信息
echo_msg(str(err_info))
print_msg(str(err_info))
# 防止exe程序执行结束闪退
os.system("pause")

0 comments on commit 947f484

Please sign in to comment.