簡體   English   中英

將多個工作簿合並為一個

[英]Combine Multiple Workbooks into One

我有各種輸入的.xlsx文件,其中包含12張紙(每個.xlsx文件中的所有紙頁都具有相同的名稱)。 我需要將這些文件合並到一個.xlsx文檔中,同時保留原始工作表的名稱,但是每張工作表的所有文檔中的數據都會附加到原始工作表中。

例如,請參閱我的原始輸出:

原始輸出

期望的輸出

當前,我沒有在任何地方添加inputFile名稱,而只是試圖合並到一個工作簿中。 但是,我不斷收到錯誤:

錯誤

def createEmptyWorkbook(self, outputFileName):
    logging.info('creating empty workbook: %s' % (outputFileName))
    # create empty workbook
    ncoa_combined_report = openpyxl.Workbook()

    # save new file
    ncoa_combined_report.save(outputFileName)

    ncoa_combined_report = openpyxl.load_workbook(filename=outputFileName)#, data_only=True)

    return ncoa_combined_report

def combine_sheets(self, inputFiles):
    logging.info('combining ncoa reports to one workbook')

    # new output files
    outputFile = os.path.join(self.processingDir, 'combined_ncoa_report.xlsx')

    # create empty workbook
    ncoa_combined_report = self.createEmptyWorkbook(outputFile)

    # get a list of sheet names created in output file
    outputSheetNames = ncoa_combined_report.sheetnames

    for inputFile in inputFiles:
        logging.info('reading ncoa report: %s' % (os.path.split(inputFile)[-1]))
        # load entire input file into memory
        input_wb = openpyxl.load_workbook(filename = inputFile)#, data_only=True)

        # get sheet name values in inputFile 
        sheets = input_wb.sheetnames

        # iterate worksheets in input file
        for worksheet in input_wb.worksheets:
            outputSheetMaxRow = 0
            currentSheet = ''
            row = ''
            column = ''

            logging.info('working on sheet: %s' % (worksheet.title))
            # check if sheet exist in output file and add if neccissary
            if not worksheet.title in outputSheetNames:
                logging.info('creating sheet: %s' % (worksheet.title))
                currentSheet = ncoa_combined_report.create_sheet(worksheet.title)
            else:
                currentSheet = worksheet.title

            ## check if default sheet name is in output
            #if 'Sheet' in outputSheetNames:
            #    ncoa_combined_report.remove_sheet(ncoa_combined_report.get_sheet_by_name('Sheet'))

            outputSheetMaxRow = currentSheet.max_row

            for row, entry in enumerate(worksheet, start=1):
                logging.info('working on row: %s' % (row))
                for cell in entry:
                    try:
                        outputSheetMaxRow = currentSheet.max_row
                        # add cell value to output file
                        #currentSheet[cell.coordinate].value
                        currentSheet.cell(row=row+outputSheetMaxRow, column=cell.column).value = cell.value #, value=cell
                    except:
                        logging.critical('could not add row:%s, cell:%s' % (row, entry))
                        raise ValueError('could not add row:%s, cell:%s' % (row, entry))

        # save new file
        ncoa_combined_report.save(outputFile)

我不確定為什么會收到錯誤或需要更新以糾正錯誤。 任何指導表示贊賞。

我想我發現了這部分代碼的問題。 我找到了可以從openpyxl.utils獲取xy,col和row的位置,這使我可以在附加位置的正確位置插入。 希望這會在將來對其他人有所幫助。

         for line, entry in enumerate(worksheet, start=1):
                #logging.info('working on row: %s' % (row))
                for cell in entry:
                    #try:
                    xy = openpyxl.utils.coordinate_from_string(cell.coordinate) # returns ('A',4)
                    col = openpyxl.utils.column_index_from_string(xy[0]) # returns 1
                    rowCord = xy[1]
                    # add cell value to output file
                    #currentSheet[cell.coordinate].value
                    if line == 1 and inputFileCount == 1:
                        currentSheet.cell(row=1, column=1).value = 'Project'
                        currentSheet.cell(row=1, column=2).value = os.path.split(inputFile)[-1]
                    if line == 1 and inputFileCount > 1:
                        currentSheet.cell(row=outputSheetMaxRow + 2, column=1).value = 'Project'
                        currentSheet.cell(row=outputSheetMaxRow + 2, column=2).value = os.path.split(inputFile)[-1]
                    else:
                        currentSheet.cell(row=outputSheetMaxRow + rowCord + 1, column=col).value = cell.value #, value=cell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM