繁体   English   中英

使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要

[英]Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python

每次运行后,我将获得一个包含测试结果的新csv文件,并且能够将所有excel文件合并为一个excel文件,每次运行都以工作表名称进行。

为此,我正在使用xlwt

他人参考的代码,用于通过以下方式将不同的excel文件添加到合并的excel文件:

book = xlwt.Workbook()
    for file in os.listdir(path):
        if file.endswith('csv'):
            sheet = book.add_sheet(file[:-4])
            with open(path + file) as filname:
                reader = csv.reader(filname)
                i = 0
                for row in reader:
                    for j, each in enumerate(row):
                        sheet.write(i, j, each)
                    i += 1

    book.save("consolidate_result.xls")

现在,我有一个方案,其中我必须在Excel的新摘要表中提供不同测试运行的摘要。

这是我的示例Excel文件,其中包含具有这些数据格式的多个工作表,其中第一列作为测试名称,第二列作为测试状态,第三列作为该测试的时间值:

具有名称Run 1Run 1

Test Name   Test Status     Time Value
Test 1      PASS            00:06:43
Test 2      Fail            00:06:24
Test 3      PASS            00:06:10
Test 4      PASS            00:05:25
Test 5      Fail            00:05:07
Test 6      PASS            00:02:45

具有名称Run 2Run 2

Test Name   Test Status     Time Value
Test 1      PASS            00:05:43
Test 2      Fail            00:04:24
Test 3      PASS            00:05:10
Test 4      PASS            00:06:25
Test 5      PASS            00:03:07
Test 6      PASS            00:04:45

具有名称Run 3Run 3

Test Name   Test Status     Time Value
Test 1      PASS            00:06:40
Test 2      PASS            00:06:52
Test 3      PASS            00:05:50
Test 4      PASS            00:05:35
Test 5      PASS            00:06:17
Test 6      PASS            00:03:55

我想要实现的是在具有这种格式的现有excel文件中获得一个名称为Status或Consolidation的新工作表

Test Name   Test-Status        Run 1        Run 2       Run 3
Test 1      Pass               00:06:43     00:05:38    00:06:43
Test 2      Fail               00:06:24    00:05:56     00:06:24
Test 3      Pass               00:06:10    00:06:43     00:06:10
Test 4      Pass               00:05:25    00:05:32     00:05:25
Test 5      Fail               00:05:07    00:05:22     00:05:07
Test 6      Pass               00:02:45    00:07:26     00:02:45

我试图通过使用pd.ExcelFile(filename)读取excel文件将结果添加到列表中,然后遍历工作表并将数据添加到结果列表中

df = pd.read_excel(fname, None)
result=[]
for x in range(len(df.keys())):
    dfx=pd.read_excel(xls, xls.sheet_names[x])
    result.append(dfx)

当我使用writer = pd.ExcelWriter(fname, engine='openpyxl')df.to_excel(writer, sheet_name='Summary') ,有人可以帮我将结果合并到新的工作表中writer = pd.ExcelWriter(fname, engine='openpyxl')添加一个名为Summary的空白表。 提前致谢

我建议使用sheet_name=None参数由所有工作sheet s创建Ordered Dictionary of DataFramesOrdered Dictionary of DataFrames

path = "file.xlsx"

df = pd.read_excel(path, sheet_name=None)
print (df)
OrderedDict([('Run 1',   Test Name Test Status Time Value
0    Test 1        PASS   00:06:43
1    Test 2        Fail   00:06:24
2    Test 3        PASS   00:06:10
3    Test 4        PASS   00:05:25
4    Test 5        Fail   00:05:07
5    Test 6        PASS   00:02:45), ('Run 2',   Test Name Test Status Time Value
0    Test 1        PASS   00:05:43
1    Test 2        Fail   00:04:24
2    Test 3        PASS   00:05:10
3    Test 4        PASS   00:06:25
4    Test 5        PASS   00:03:07
5    Test 6        PASS   00:04:45), ('Run 3',   Test Name Test Status Time Value
0    Test 1        PASS   00:06:40
1    Test 2        PASS   00:06:52
2    Test 3        PASS   00:05:50
3    Test 4        PASS   00:05:35
4    Test 5        PASS   00:06:17
5    Test 6        PASS   00:03:55)])

然后循环和concat与列对齐一起Test NameTest Status ,所以set_index是必要的。 还为不匹配的值添加NaN

d = {k:v.set_index(['Test Name','Test Status'])['Time Value'] for k, v in df.items()}
result= pd.concat(d, axis=1).reset_index()
print (result)
  Test Name Test Status     Run 1     Run 2     Run 3
0    Test 1        PASS  00:06:43  00:05:43  00:06:40
1    Test 2        Fail  00:06:24  00:04:24       NaN
2    Test 2        PASS       NaN       NaN  00:06:52
3    Test 3        PASS  00:06:10  00:05:10  00:05:50
4    Test 4        PASS  00:05:25  00:06:25  00:05:35
5    Test 5        Fail  00:05:07       NaN       NaN
6    Test 5        PASS       NaN  00:03:07  00:06:17
7    Test 6        PASS  00:02:45  00:04:45  00:03:55

最后追加到新工作表中的现有文件中:

#https://stackoverflow.com/a/42375263
from openpyxl import load_workbook

book = load_workbook(path)
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book

result.to_excel(writer, sheet_name = 'Status', index=False)

writer.save()
writer.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM