MacOS_Parsers/Processing/0_01_слияние_всех эксель файлов из папки в один одинаковый формат.py
2025-07-31 13:29:40 +03:00

31 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
import os
# Укажите путь к папке, где находятся все файлы Excel
folder_path = '/Users/valis/Yandex.Disk.localized/Python3/Parsing ZARAHOME/src_2024-09-05/records_folder'
# Укажите путь и имя нового файла Excel, в который будут скопированы все строки
output_file = os.path.join(folder_path, 'Allfile.xlsx')
# Считываем все имена файлов в папке
files = [f for f in os.listdir(folder_path) if f.endswith('.xlsx') or f.endswith('.xls')]
# Флаг для определения, является ли текущий файл первым (для записи заголовков)
first_file = True
# Открываем файл Excel для записи
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
for file in files:
file_path = os.path.join(folder_path, file)
try:
df = pd.read_excel(file_path, engine='openpyxl')
# Если это первый файл, записываем с заголовками
if first_file:
df.to_excel(writer, index=False, sheet_name="Sheet", startrow=0, header=True)
first_file = False
else:
# Иначе записываем без заголовков, продолжая с последней строки
df.to_excel(writer, index=False, sheet_name="Sheet", startrow=writer.sheets["Sheet"].max_row, header=False)
except Exception as e:
print(f"Ошибка при обработке файла {file}: {e}")