![](/img/trans.png)
[英]How to append all sheets in multiple Excel files into One Excel file (not consolidating or combining them into one sheet)
[英]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 1
表Run 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
表Run 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
表Run 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 DataFrames
的Ordered 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 Name
和Test 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.