簡體   English   中英

使用python將范圍從多個工作簿復制到主工作簿中的新工作表

[英]Copy range from multiple workbooks to new worksheets in a master workbook using python

我有一堆文件,我正在運行一個循環來做一些計算。 這個循環已經完成,但我將結果保存到新文件中,根據原始文件。

    file1.xlsx
    file2.xlsx

運行代碼后:

    results/file1_results.xlsx
    results/file2_results.xlsx

這是文件循環:

    directory = os.getcwd()  
    
    for filename in os.listdir(directory):
    if filename.endswith(".xlsx"):
        wb = load_workbook(filename)  
        ws = wb.active
        max_row = ws.max_row
        
        ws["CX1"] = "Humanas"
        for row in range(2, max_row + 1):
            ws["CX{}".format(row)] = round((
                                    ws['R{}'.format(row)].value + 
                                    ws['U{}'.format(row)].value + 
                                    ws['X{}'.format(row)].value + 
                                    ws['AA{}'.format(row)].value + 
                                    ws['AD{}'.format(row)].value + 
                                    ws['AG{}'.format(row)].value + 
                                    ws['AJ{}'.format(row)].value + 
                                    ws['AM{}'.format(row)].value ) * (50/8))

        ws["CY1"] = "Exatas"
        for row in range(2, max_row + 1):
            ws["CY{}".format(row)] = round((
                                    ws['AP{}'.format(row)].value + 
                                    ws['AS{}'.format(row)].value + 
                                    ws['AV{}'.format(row)].value + 
                                    ws['AY{}'.format(row)].value ) * (50/4))

        ws["CZ1"] = "Linguagens"
        for row in range(2, max_row + 1):
            ws["CZ{}".format(row)] = round((
                                    ws['BB{}'.format(row)].value + 
                                    ws['BE{}'.format(row)].value + 
                                    ws['BH{}'.format(row)].value + 
                                    ws['BK{}'.format(row)].value + 
                                    ws['BN{}'.format(row)].value + 
                                    ws['BQ{}'.format(row)].value + 
                                    ws['BT{}'.format(row)].value + 
                                    ws['BW{}'.format(row)].value + 
                                    ws['BZ{}'.format(row)].value + 
                                    ws['CC{}'.format(row)].value + 
                                    ws['CF{}'.format(row)].value + 
                                    ws['CI{}'.format(row)].value ) * (50/12))

        ws["DA1"] = "Biológicas"
        for row in range(2, max_row + 1):
            ws["DA{}".format(row)] = round((
                                    ws['CL{}'.format(row)].value + 
                                    ws['CO{}'.format(row)].value + 
                                    ws['CR{}'.format(row)].value + 
                                    ws['CU{}'.format(row)].value ) * (50/4))

        wb.save('results/' + os.path.splitext(filename)[0] + '_results.xlsx')
        wb.close

    else:
        continue

數據是一堆傻瓜(0或1)

我需要將結果調整為單個 file.xlsx。 我需要獲得多個工作表(命名為原始文件或接近於此)。 我不想合並成一個 ws

我正在嘗試復制所有 results.xlsx 的范圍並將其放入新文件中。 但沒有成功。 一個很好的選擇是跳過所有文件的創建並將結果直接放入最后一個,但我不知道如何做到這一點。

編輯 1 - 成功加入特定工作表中的所有結果,但現在,我需要清理所有結果以僅獲得結果。

dest_wb = Workbook()
from openpyxl import Workbook
from openpyxl import load_workbook

for root, dir, filenames in os.walk(path):
    for file in filenames:
        file_name = file.split('.')[0]
        file_path = os.path.abspath(os.path.join(root, file))
    
        # Create new sheet in destination Workbook
        dest_wb.create_sheet(file_name)
        dest_ws = dest_wb[file_name]

        source_wb = load_workbook(file_path)
        source_sheet = source_wb.active
        for row in source_sheet.rows:
            for cell in row:
                dest_ws[cell.coordinate] = cell.value


dest_wb.save("results/report.xlsx")

這樣做的結果:

dados = pd.read_excel("results/report.xlsx", sheet_name=None)
df = pd.concat(dados[frame] for frame in dados.keys())

lista_colunas = [7, 10, 101, 102, 103, 104]
filtro = df.columns[lista_colunas]
final_df = df[filtro]

grouped_df = final_df.groupby(final_df.columns[1])
salas = grouped_df.groups.keys()

writer = pd.ExcelWriter('results/resultado.xlsx', engine='xlsxwriter')
for sala in salas:
        splitdf = grouped_df.get_group(sala) 
        splitdf.to_excel(writer, sheet_name=str(sala)) 
writer.save()

暫無
暫無

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

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