我正在尝试扫描一些 excel 文件并复制数据,但数据没有被复制到新的工作表中,无法弄清楚为什么

问题描述 投票:0回答:0

我写了一个程序,它读取一个包含 excel 工作表的文件夹,并且应该复制它为工作表中的特定 x 和 y 标题找到的数据。我已经浏览过表格并确保信息在那里,但应该将其复制到的表格仍然空白,谁能解释为什么会这样?我已经编写了一些调试代码来尝试显示它是否正确读取了表格并且它似乎正在这样做。任何帮助将不胜感激。谢谢

这是我写的代码:

from openpyxl import load_workbook, Workbook
import os
from datetime import datetime
from tqdm import tqdm

# Set up paths to input and output files
scan_folder_path = os.path.expanduser("c:\\Users\\n.margot\\Desktop\\scan")
output_file_path = os.path.expanduser("c:\\Users\\n.margot\\Desktop\\m2m.xlsx")

# Set up dictionary of headings
headings = {
    "Brent Swaps": [datetime(2023, 6, 1), datetime(2023, 7, 1), datetime(2023, 8, 1), datetime(2023, 9, 1), datetime(2023, 10, 1), datetime(2023, 11, 1), datetime(2023, 12, 1)],
    "EBOB": [datetime(2023, 4, 1), datetime(2023, 7, 1), datetime(2023, 8, 1), datetime(2023, 9, 1)],
    "Crack": [datetime(2023, 7, 1), datetime(2023, 8, 1), datetime(2023, 9, 1), datetime(2024, 1, 1)],
    "F/P": [datetime(2023, 7, 1), datetime(2023, 8, 1), datetime(2023, 9, 1)],
}

# Format dates to only include year and month
for key in headings:
    headings[key] = [date.strftime("%Y-%m") for date in headings[key]]

# Load the output workbook
if os.path.exists(output_file_path):
    output_wb = load_workbook(output_file_path)
else:
    output_wb = Workbook()

# Set the active sheet in the output workbook
output_sheet = output_wb.active

# Loop over files in scan folder
for file_name in tqdm(os.listdir(scan_folder_path), desc="Processing Files"):
    if file_name.endswith(".xlsx"):
        # Load the input workbook
        input_wb = load_workbook(os.path.join(scan_folder_path, file_name))

        # Loop over sheets in the input workbook
        for sheet_name in input_wb.sheetnames:
            print(f"Processing sheet {sheet_name} in file {file_name}")
            input_sheet = input_wb[sheet_name]
            # Loop over headings and copy data to output sheet
            for x_heading, y_headings in headings.items():
                x_col = None
                y_cols = []
                for col in range(1, input_sheet.max_column + 1):
                    cell = input_sheet.cell(row=1, column=col)
                    if cell.value == x_heading:
                        x_col = col
                    elif cell.value:
                        try:
                            date_val = datetime.strptime(str(cell.value), '%d/%m/%Y').date()
                            if date_val.replace(day=1) in y_headings:
                                y_cols.append(col)
                        except ValueError:
                            pass

                # If the x and y headings were found, copy the data to the output sheet
                if x_col is not None and y_cols:
                    print(f"Copying data from {sheet_name} ({x_heading})...")
                    for row in range(2, input_sheet.max_row + 1):
                        x_value = input_sheet.cell(row=row, column=x_col).value
                        if x_value is not None:
                            output_sheet.cell(row=len(output_sheet['A']) + 1, column=1).value = sheet_name
                            output_sheet.cell(row=len(output_sheet['A']), column=2).value = x_heading
                            output_sheet.cell(row=len(output_sheet['A']),column=3).value = x_value
                            for y_col in y_cols:
                                y_value = input_sheet.cell(row=row, column=y_col).value
                                if y_value is not None:
                                 y_heading = input_sheet.cell(row=1, column=y_col).value
                                 output_sheet.cell(row=len(output_sheet['A']), column=4).value = y_heading
                                 output_sheet.cell(row=len(output_sheet['A']), column=5).value = y_value



# Save the output workbook
output_wb.save(output_file_path)
print(f"Data has been copied to {output_file_path}")

这是调试:

C:\Users\n.margot\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\n.margot\PycharmProjects\pythonProject\main.py 
Processing Files:   0%|          | 0/4 [00:00<?, ?it/s]Processing sheet Sheet1 in file Curves 240323.xlsx
Processing sheet Sheet2 in file Curves 240323.xlsx
Processing sheet Sheet3 in file Curves 240323.xlsx
Processing sheet Sheet4 in file Curves 240323.xlsx
Processing sheet Sheet5 in file Curves 240323.xlsx
Processing sheet Sheet6 in file Curves 240323.xlsx
Processing sheet Market in file The COB-Advanced 20230324.xlsx
Processing sheet Rollover in file The COB-Advanced 20230324.xlsx
Processing Files: 100%|██████████| 4/4 [00:00<00:00, 26.74it/s]
Data has been copied to c:\Users\n.margot\Desktop\m2m.xlsx

Process finished with exit code 0
excel openpyxl
© www.soinside.com 2019 - 2024. All rights reserved.