两个Excel表实现同步。
1、打开我们需要设置的 Excel表格(主动表),选中我们需要同步的数据单元格,右击鼠标选择“复制”。
2、右击鼠标打开需要数据同步的 Excel表格(从动表),选中
you're not kidding?同步操作是那么的麻烦,直接把它自动化不香吗?看我分解
谨记以自动化工具为荣、人肉操作为耻。注意:要被操作的表,在下文中称为源Excel表
一、你需求提出的同步操作缺点
同步操作虽然可以一次操作多个Excel的目的,但那仍然属于人肉操作,因为你要手动去操作一次,还得每个Excel都去ctrl+s一次,1000个Excel操作完累不累
手动操作就意味着可能会发生操作失误
根据你的描述得出以下两点(满足自动化基本要求):
你的这个操作是规律性、重复性的
你的Excel原表和输出表的结构也有规律(结构是一致的)
ERP系统估计你这没办法修改导出功能源码,所以以下方案都是从你这端作为出发点
二:自动化方案(一次编写,一劳永逸)
将下载到本地的源Excel统一放到一个目录,双击exe程序,就将修改完成后的Excel按1-
20.xlsx统一
输出到另外一个目录了(这样够不够香?),并在当前路径下生成一个日志文件,标识源Excel-->目标Excel的对应关系,便于以后查询or排错
详细技术方案:
Python+pandas库轻松解决,下面是一个我之前批量修改excel的脚本案例(源码在最后,但是得根据你的Excel结构和操作单独编写脚本):
将源Excel存在在脚本目录中的MAC目录下,如图
执行 Excel操作脚本
.py
(可以打包为exe)后生成了一个新目录new_mac,
run.log中记录和转换对应关系,源表-->新名字
打开Excel验证转换是否成功,源Excel结构:
修改后的结构:
源码:
import os
import pandas as pd
import logging
logging.basicConfig(filename='run.log', format='[%(levelname)s%(asctime)s]%(message)s',
datefmt='%Y-%m-%d%H:%M:%S', level=logging.INFO, filemode='w')
def formar_excel(current_mac_path, new_name):
# 读取excel数据
data = pd.read_excel(current_mac_path)
# d定义列更改后的名字
rename = {
'数据表格列序号': 'No',
'SN': 'SN',
'MAC': 'MAC'
}
new_dic = {}
# 获取excel指定列
dic = data.loc[:, [x for x in rename]].to_dict()
# 生成pandas使用的dataframe
for key in dic:
test_list = []
for k, v in dic[key].items():
test_list.append(v)
new_dic[rename[key]] = test_list
# 生成新excel
writeer = pd.ExcelWriter(f'{new_name}.xlsx')
df1 = pd.DataFrame(new_dic)
df1 = df1.astype('str')
df1.to_excel(writeer, index=False)
# 保存
writeer.save()
# 获取路径
new_excel_path = './new_mac/'
mac_path = os.path.abspath('./mac')
try:
if os.path.exists(new_excel_path):
old_file = os.listdir(new_excel_path)
for old_file in old_file:
old_file = os.path.join(new_excel_path, old_file)
os.remove(old_file)
logging.info(f'删除旧文件:{old_file}')
os.rmdir(new_excel_path)
except Exception as e:
logging.error(f'删除旧文件出错: {e}')
finally:
os.mkdir(new_excel_path)
try:
logging.info('开始转换Excel')
if os.path.exists(mac_path):
excel_list = os.listdir(mac_path)
new_name = 0
for excel in excel_list:
new_name += 1
current_mac_path = os.path.join(mac_path, excel)
formar_excel(current_mac_path, f'{new_excel_path}{new_name}')
logging.info(f'{ excel } ------------> { new_name }.xlsx')
logging.info('Excel 转换完成')
else:
logging.error(f'读取{mac_path}目录失败,已自动创建目录,已经Excel表添加到此目录中')
os.mkdir(mac_path)
except Exception as e:
logging.error(f'ERROR:{e}')