当前位置:   article > 正文

python 合并多个excel文件_python合并多个excel

python合并多个excel

使用 openpyxl

思路:

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

安装软件

pip install openpyxl
  • 1

源代码:

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

效果截图:

在这里插入图片描述

使用 pandas

思路:

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

安装软件:

pip install pandas
  • 1

源代码:

#!/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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

参考

  • 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
  • 二维数组转置

源代码位置:

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号