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

时间:2018-08-20 05:49:56

标签: python excel pandas openpyxl

每次运行后,我都会得到一个包含测试结果的新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 1的第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 2的第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 3的第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文件中获得一个名称为“状态或合并结果”的新工作表

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')时,它会覆盖excel并添加一个名称为Summary的空白表。 预先感谢

1 个答案:

答案 0 :(得分:0)

我建议使用UnhandledPromiseRejectionWarning: MissingSchemaError: Schema hasn't been registered for model "User". Use mongoose.model(name, schema) at new MissingSchemaError (/home/pct26/Rushabh/Project/RevstancePhaseII/RST/myapp/node_modules/mongoose/lib/error/missingSchema.js:20:11) at NativeConnection.Connection.model (/home/pct26/Rushabh/Project/RevstancePhaseII/RST/myapp/node_modules/mongoose/lib/connection.js:791:11) 参数创建所有func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("userInfo:-> \(userInfo)") let redirect_flag = userInfo["redirect_flag"]as! String if application.applicationState == .inactive { //MARK: - code here to redirect when app is not in background or inactive if (UserDefaults.standard.value(forKey: KEY_IS_LOGIN) != nil) { switch redirect_flag { case "1": print("redirect controller") case "2": print("redirect controller") case "3": print("redirect controller") default: break } } }else { //MARK: - code here to redirect when app is in background or active mode switch redirect_flag { case "1": print("redirect controller") case "2": print("redirect controller") case "3": print("redirect controller") default: break } } } 的{​​{1}}:

sheet_name=None

然后循环和concat并按列Ordered Dictionary of DataFramessheet对齐,因此set_index是必需的。还为不匹配的值添加了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)])

Test Name

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

Test Status