【问题标题】:Merge same name sheets from various excels into one将各种excel中的同名表合并为一张
【发布时间】:2019-09-13 16:55:31
【问题描述】:

我正在尝试将多个具有相同名称的 Excel 中的工作表合并到一个 Excel 工作表中

试过下面的代码:

import os
import pandas as pd

files = os.listdir("XXXX")

print("All files in the given directory are :", files)

files_xlsx = [f for f in files if f[-4:] == 'xlsx']

print("Excel files in this directory are :", files_xlsx)

y = "XXXX"

excels = [pd.ExcelFile(y +'\\'+ name) for name in files_xlsx]

for each in excels:
    sheets =pd.ExcelFile(each).sheet_names

print ("Sheet names in these excel files are : ", sheets)

NSheets = len(sheets)

print ("Number of different Sheet names in these excel files are : ", NSheets)

for z in sheets:
        frames = [x.parse(x.sheet_names[i], header=None,index_col=None) for x in excels]
        frames[1:] = [df[1:] for df in frames[1:]]
        combined = pd.concat(frames)
        combined.to_excel("XXXX\\Output_" + z + ".xlsx", header=False, index=False)

这会导致使用不同的工作表名称创建多个文件,但所有这些工作表都具有与工作表 1 中相同的数据。数据不正确。

【问题讨论】:

  • 在你的frames = [x.parse(x.sheet_names[i], header=None,index_col=None) for x in excels],你在哪里设置i变量?
  • @prebsus 抱歉。那是我正在尝试的事情。它实际上是` frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels] ` 我知道这将始终返回第一张表。但我不知道如何根据工作表名称创建搜索,以便所有 excel 文件的相同工作表名称仅组合
  • 好的,感谢您的澄清。我已尝试在下面的答案中包含您在此处查找的内容(匹配 sheet name 而不是 sheet position

标签: excel python-3.x


【解决方案1】:

我不能 100% 确定我是否完全理解您的问题,但我按照您的需要阅读了每个唯一的 Excel 工作表名称输入的 1 个输出 Excel 文件。如果是这样,那么试试这个:

import os
import pandas as pd

files = os.listdir("XXXX")

print("All files in the given directory are :", files)

files_xlsx = [f for f in files if f[-4:] == 'xlsx']

print("Excel files in this directory are :", files_xlsx)

y = "XXXX"

excels = [pd.ExcelFile(y +'\\'+ name) for name in files_xlsx]

for each in excels:
    sheets =pd.ExcelFile(each).sheet_names


print ("Sheet names in these excel files are : ", sheets)

NSheets = len(sheets)

print ("Number of different Sheet names in these excel files are : ", NSheets)

for z in sheets:
    frames = list()
    for x in excels:
        try:
            x.sheet_names.index(z)
            frames.append(x.parse(x.sheet_names.index(z), header=None, index_col=None))
            frames[1:] = [df[1:] for df in frames[1:]]  # keep only header from first file for each sheetname
            combined = pd.concat(frames)
            combined.to_excel("XXXX\\Output_" + z + ".xlsx", header=False, index=False)
        except:
          pass

如果我正确理解了您的问题,那么这应该可以满足您的要求。

PS:我添加了之前取出的行(只保留第一个标题),它似乎符合我的小测试样本的要求

【讨论】:

  • 您好,非常感谢您的回答。我才一个月才开始学习python,所以我很难理解如何解决这个问题,但是多亏了你,我的问题几乎解决了。但是,我仍然面临一个问题。我的数据集是这样的,第一行是每张表中的标题,然后我在后续行中有数据。此外,具有相同名称的工作表的标题是一致的。所以我的要求是只有一个标题行,然后将后续工作表中的数据附加到同一张工作表中,而没有第 1 行的标题
  • 当使用你给出的代码时,当这些工作表组合在一起时,每次添加新工作表时都会生成标题行(即第 1 行)我使用这一行来摆脱标题,即除第一张以外的所有工作表的第一行 ---> frames[1:] = [df[1:] for df in frames[1:]] 即保留标题行,即第 1 行的一张工作表仅第一个工作簿并删除标题行,即所有其他要附加的工作表的第 1 行。但是,它无法正常工作。对于我的第二张工作表,追加了 2 行,我不确定为什么
  • 嗨@prebsus,感谢您的回答,但是您添加回来的代码行在数据行数方面存在问题。例如,如果我有 4 个具有相同工作表名称的工作簿,每个工作簿都有 10 个数据行,不包括标题,那么在输出工作簿中,工作表包含总共 38 个数据行,即标题列,加上第一个工作簿的 10 个数据行,加上 8第二个工作簿的行,第三个工作簿的 9 个数据行和第四个工作簿的 10 个数据行
  • 我没有得到相同的结果,但我稍后会看看这个。
猜你喜欢
  • 2019-11-15
  • 2016-11-13
  • 2016-09-08
  • 2014-10-04
  • 2019-06-27
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多