我正在尝试编写一个复制数据的 python 程序,但在编写循环时不断出现缩进错误

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

我正在尝试编写一个 python 程序来复制数据,但在编写循环时不断出现缩进错误并且无法工作/不明白为什么?

谁能给我一些指导?

提前谢谢你。

`

import openpyxl
import os
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 = {
    "EBOB": ["Apr-23", "Jul-23", "Aug-23", "Sep-23"],
    "Brent Swaps": ["Jun-23", "Jul-23", "Aug-23", "Sep-23", "Oct-23", "Nov-23", "Dec-23"],
    "EOB Crack": ["Jul-23", "Aug-23", "Sep-23", "Cal 24"],
    "Naphtha F/P": ["Jul-23", "Aug-23", "Sep-23"],
}

# Load the output workbook
if os.path.exists(output_file_path):
    output_wb = openpyxl.load_workbook(output_file_path)
else:
    output_wb = openpyxl.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 = openpyxl.load_workbook(os.path.join(scan_folder_path, file_name))
        input_sheet = input_wb.active

    # Loop over headings and copy data to output sheet
    for x_heading, y_headings in headings.items():
        # Find the row and column numbers for the x and y headings
        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 in y_headings:
               y_cols.append(col)

        # If the x and y headings were found, copy the data to the output sheet
        if x_col is not None and y_cols:
            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=row, column=1).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:
                            output_sheet.cell(row=row, column=y_col - y_cols[0] + 2).value = y_value

# Save the output workbook with progress bar
with tqdm(desc="Saving...") as progress:
    output_wb.save(output_file_path)
    progress.update(1)`

这是我得到的错误: `

`>>> import os
>>> 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 = {
...     "EBOB": ["Apr-23", "Jul-23", "Aug-23", "Sep-23"],
...     "Brent Swaps": ["Jun-23", "Jul-23", "Aug-23", "Sep-23", "Oct-23", "Nov-23", "Dec-23"],
...     "EOB Crack": ["Jul-23", "Aug-23", "Sep-23", "Cal 24"],
...     "Naphtha F/P": ["Jul-23", "Aug-23", "Sep-23"],
... }
>>>
>>> # Load the output workbook
>>> if os.path.exists(output_file_path):
...     output_wb = openpyxl.load_workbook(output_file_path)
... else:
...     output_wb = openpyxl.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 = openpyxl.load_workbook(os.path.join(scan_folder_path, file_name))
...         input_sheet = input_wb.active
...
Processing Files: 100%|██████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 33.09it/s]
>>>         # Loop over headings and copy data to output sheet
>>>         for x_heading, y_headings in headings.items():
  File "<stdin>", line 1
    for x_heading, y_headings in headings.items():
IndentationError: unexpected indent
>>>             # Find the row and column numbers for the x and y headings
>>>             x_col = None
  File "<stdin>", line 1
    x_col = None
IndentationError: unexpected indent
>>>             y_cols = []
  File "<stdin>", line 1
    y_cols = []
IndentationError: unexpected indent
>>>             for col in range(1, input_sheet.max_column + 1):
  File "<stdin>", line 1
    for col in range(1, input_sheet.max_column + 1):
IndentationError: unexpected indent
>>>                 cell = input_sheet.cell(row=1, column=col)
  File "<stdin>", line 1
    cell = input_sheet.cell(row=1, column=col)
IndentationError: unexpected indent
>>>                 if cell.value == x_heading:
  File "<stdin>", line 1
    if cell.value == x_heading:
IndentationError: unexpected indent
>>>                     x_col = col
  File "<stdin>", line 1
    x_col = col
IndentationError: unexpected indent
>>>                 elif cell.value in y_headings:
  File "<stdin>", line 1
    elif cell.value in y_headings:
IndentationError: unexpected indent
>>>                     y_cols.append(col)
  File "<stdin>", line 1
    y_cols.append(col)
IndentationError: unexpected indent
>>>
>>>             # If the x and y headings were found, copy the data to the output sheet
>>>             if x_col is not None and y_cols:
  File "<stdin>", line 1
    if x_col is not None and y_cols:
IndentationError: unexpected indent
>>>                 for row in range(2, input_sheet.max_row + 1):
  File "<stdin>", line 1
    for row in range(2, input_sheet.max_row + 1):
IndentationError: unexpected indent
>>>                     x_value = input_sheet.cell(row=row, column=x_col).value
  File "<stdin>", line 1
    x_value = input_sheet.cell(row=row, column=x_col).value
IndentationError: unexpected indent
>>>                     if x_value is not None:
  File "<stdin>", line 1
    if x_value is not None:
IndentationError: unexpected indent
>>>                         output_sheet.cell(row=row, column=1).value = x_value
  File "<stdin>", line 1
    output_sheet.cell(row=row, column=1).value = x_value
IndentationError: unexpected indent
>>>                         for y_col in y_cols:
  File "<stdin>", line 1
    for y_col in y_cols:
IndentationError: unexpected indent
>>>                             y_value = input_sheet.cell(row=row, column=y_col).value
  File "<stdin>", line 1
    y_value = input_sheet.cell(row=row, column=y_col).value
IndentationError: unexpected indent
>>>                             if y_value is not None:
  File "<stdin>", line 1
    if y_value is not None:
IndentationError: unexpected indent
>>>                                 output_sheet.cell(row=row, column=y_col - y_cols[0] + 2).value = y_value
  File "<stdin>", line 1
    output_sheet.cell(row=row, column=y_col - y_cols[0] + 2).value = y_value
IndentationError: unexpected indent
>>>
>>> # Save the output workbook with progress bar
>>> with tqdm(desc="Saving...") as progress:
...     output_wb.save(output_file_path)
...     progress.update(1)``

我希望它能找到 x 和 y 标题并将数据复制到另一个电子表格,从而节省我每天早上浏览电子表格寻找数字的时间

python loops openpyxl tqdm
© www.soinside.com 2019 - 2024. All rights reserved.