在工作中经常要整理一段时间的销售报表,手动下载 + 开开合合 + 复制粘贴……花费了我一大堆时间,于是决定找 Python 大人来帮一下忙。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2020/1/10 22:08
# author: Hush
import os
import glob
import pandas as pd
yewu_data = pd.DataFrame()
row_num = 0
folder_path = r'C:\Users\zeiger\Desktop\year\*' # 修改文件的位置
folder = glob.glob(folder_path)
for i in folder:
# print(i)
excel_list = os.listdir(i)
print('文件夹%s下有%d张excel表:' % (str(i.split('\\')[5]), len(excel_list)), excel_list)
for j in excel_list:
# print(j)
excel_path = i + '\\' + str(j)
data1 = pd.read_csv(excel_path, skiprows=0)
rows = data1.shape[0]
row_num = row_num + rows
data1['店铺'] = excel_path.split('\\')[5] # 后面需要将这两列调到第一二列
data1['时间'] = '2020-01-' + str(j.split('.')[0])
# print(row_num)
yewu_data = pd.concat([yewu_data, data1])
# print(yewu_data)
yewu_data.to_excel(folder_path.replace(r'*', '') + r'all.xlsx', index=False)
print('联合完毕')
print('新增行数:', row_num)
回去再码……
原文地址:https://www.cnblogs.com/wyy1480/p/12196145.html
时间: 2024-11-04 05:37:02