python-合并同格式excel

这是一个使用Python进行Excel文件合并的脚本,通过递归遍历指定目录下的所有Excel文件,并使用pandas库进行数据读取与拼接。脚本首先获取指定路径下的所有Excel文件路径,然后读取每个文件,将数据合并到一个新的Excel文件中,确保数据精度并统一转换为字符串格式。
摘要由CSDN通过智能技术生成

简单给别人写的脚本记录一下。

import os
import pandas as pd

source_path = 'D:\\projects\\excel_merge\\test_source'
dist_file_path = '.\\text.xlsx'


# 递归获取目录(文件夹)下的所有文件路径


def get_file_path(dir_path):
    res_file_path_list = []

    def get_filepath_recur(dir_path, list_name):
        """递归获取目录下(文件夹下)所有文件的路径"""
        for file in os.listdir(dir_path):  # 获取文件(夹)名
            file_path = os.path.join(dir_path, file)  # 将文件(夹)名补全为路径
            if os.path.isdir(file_path):  # 如果是文件夹,则递归
                get_filepath_recur(file_path, list_name)
            else:
                list_name.append(file_path)  # 保存路径
        return list_name

    res = get_filepath_recur(dir_path, res_file_path_list)
    return res

# for i in get_file_path(source_path):
#     print(i)


# path:所有需要合并的excel文件所在的文件夹
def read_merge_write(file_path_list, dist_file_path):
    print(file_path_list)
    df_list = []
    for file_path in file_path_list:
        df = pd.read_excel(file_path, engine='openpyxl')
        df_list.append(df)
    df_merged = pd.concat(df_list, axis=0)
    # 预防数据精度缺失问题,统一都转为字符串格式
    for col_name, series in df_merged.iteritems():
        df_merged[col_name] = df_merged[col_name].astype(str)
    writer = pd.ExcelWriter(dist_file_path)
    df_merged.to_excel(writer, sheet_name='res', startcol=0, index=False)
    writer.save()
    return 0


read_merge_write(get_file_path(source_path), dist_file_path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值