python 合并多个excel文件

使用 openpyxl

思路:

  • 读取n个excel的文件,存储在一个二维数组中,注意需要转置。
  • 将二维数组的数据写入excel。

安装软件:

pip install openpyxl

源代码:

import os
import openpyxl
# 将n个excel文件数据合并到一个excel


# 读取n个excel文件数据,并且合并到一个二维数组,每个excel只读取A列,且行数保持一样
def merge(n):
    data = []
    for i in range(n):
        data_file_path = os.path.join('data', f'data{i + 1}.xlsx')
        # 返回一个workbook数据类型的值
        workbook = openpyxl.load_workbook(data_file_path)
        sheet = workbook.active
        # 取A列数据
        cell = sheet['A']
        column = []
        for j in cell:
            column.append(j.value)
        data.append(column)

    # print(data)
    # 转置
    transpose_data = list(map(list, zip(*data)))
    # print(transpose_data)
    merge_file_path = os.path.join('data', 'merge.xlsx')
    save(transpose_data, merge_file_path)


# 将二维数据数据保存到excel文件
def save(data, file_path):
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = 'Sheet1'
    workbook.save(file_path)

    for row in data:
        sheet.append(row)
    workbook.save(file_path)


if __name__ == '__main__':
    merge(10)

效果截图:

在这里插入图片描述

使用 pandas

思路:

  • 读取n个excel的文件,存储在一个二维数组中,注意需要转置。
  • 将二维数组的数据写入excel。

安装软件:

pip install pandas

源代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import pandas as pd
import os

# 将n个excel文件数据合并到一个excel


# 读取n个excel文件数据,并且合并到一个二维数组,每个excel只读取A列,且行数保持一样
def merge(n):
    data = []
    for i in range(n):
        data_file_path = os.path.join('data', f'data{i + 1}.xlsx')
        df = pd.read_excel(data_file_path, index_col=None, header=None, sheet_name='Sheet1')
        # 仅获取第0列数据
        data.append(df.values[:, 0])

    # print(data)
    # 转置
    transpose_data = list(map(list, zip(*data)))
    # print(transpose_data)
    merge_file_path = os.path.join('data', 'merge.xlsx')
    save(transpose_data, merge_file_path)


# 将二维数据数据保存到excel文件
def save(data, file_path):
    df = pd.DataFrame(data)
    # 写入本地excel文件
    df.to_excel(file_path, sheet_name="Sheet1", index=False, header=False)


# main函数
if __name__ == '__main__':
    merge(10)

参考

  • https://zhuanlan.zhihu.com/p/353669230
  • https://blog.csdn.net/weixin_44288604/article/details/120731317
  • https://zhuanlan.zhihu.com/p/363810440
  • https://baijiahao.baidu.com/s?id=1737230506657192730&wfr=spider&for=pc
  • 二维数组转置

源代码位置:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值