如何写入多个表格到一个新的Excel中,而不会相互覆盖?

我试图把多个excel的列A写入一个新的excel列A(假设所有的excel都有一个工作表)。我已经写了一些代码,可以将一个excel的列A写入新的excel列A; 但如果有多个excel,新的excel的列A将被覆盖多次。 那么我怎样才能把所有的栏目一个接一个添加到新的excel表格中而不会相互覆盖呢? 以下是我的代码:

import os, openpyxl path = os.getcwd() def func(file): for file in os.listdir(path): if file.endswith('.xlsx'): wb = openpyxl.load_workbook(file) sheet = wb.active colA = sheet['A'] wb = openpyxl.Workbook() r = 1 for i in colA: sheet = wb.active sheet.cell(row=r, column=1).value = i.value r += 1 wb.save('new.xlsx') func(file) 

非常感谢!!

你可以举例如下:

 import os, openpyxl path = os.getcwd() def func(outputFile): c = 0 #create output workbook wbOut = openpyxl.Workbook() sheetOut = wbOut.active for fName in os.listdir(path): if fName.endswith('.xlsx'): c += 1 #move to the next column in output wb = openpyxl.load_workbook(fName) sheet = wb.active #input sheet #for r in range(1, sheet.max_row+1): # sheetOut.cell(row=r, column=c).value = sheet.cell(row = r, column = 1).value for r, cell in enumerate(sheet['A']): sheetOut.cell(row = r+1, column = c).value = cell.value wbOut.save(outputFile) #"concatenate" all columns A into one single column def funcAppend(outputFile): wbOut = openpyxl.Workbook() sheetOut = wbOut.active r = 1 for fName in os.listdir(path): if fName.endswith('.xlsx'): wb = openpyxl.load_workbook(fName) sheet = wb.active for cell in sheet['A']: sheetOut.cell(row = r, column = 1).value = cell.value r += 1 wbOut.save(outputFile) func('test.xlsx')