| | |
| | | from openpyxl import load_workbook |
| | | from datetime import datetime |
| | | import os |
| | | import random |
| | | import shutil |
| | | import string |
| | | import threading |
| | | import time |
| | | |
| | | from datetime import datetime |
| | | from openpyxl import load_workbook |
| | | from Log import logger |
| | | |
| | | |
| | | def clear_blank_rows(sheet): |
| | |
| | | [source_sheet.cell(row=row, column=col).value for col in range(1, source_sheet.max_column + 1)]) |
| | | |
| | | |
| | | def run_conformity(): |
| | | def delete_file_after_delay(file_path, delay_minutes): |
| | | delay_seconds = delay_minutes * 60 |
| | | |
| | | def delete_file(): |
| | | time.sleep(delay_seconds) |
| | | try: |
| | | if os.path.exists(file_path): |
| | | os.remove(file_path) |
| | | except Exception as e: |
| | | logger.error(f"定时删除Excel文件时发生错误: {e}") |
| | | |
| | | threading.Thread(target=delete_file).start() |
| | | |
| | | |
| | | def run_conformity(file_path, print_path): |
| | | try: |
| | | # 加载模板文件 |
| | | template_path = os.path.join('app', 'utils', 'excelmerge', '国网上海电力整合模版.xlsx') |
| | | template_excel = load_workbook(template_path) |
| | | EXCEL_FILES_PATH = os.path.join('data', 'output') |
| | | |
| | | template_sheets = {sheet.title: sheet for sheet in template_excel} |
| | | |
| | | source_folder = os.path.join('data', 'source') |
| | | source_files = [f for f in os.listdir(source_folder) if f.endswith('.xlsx') and not f.startswith('~$')] |
| | | source_files = [f for f in os.listdir(file_path) if f.endswith('.xlsx') and not f.startswith('~$')] |
| | | |
| | | for file in source_files: |
| | | source_path = os.path.join(source_folder, file) |
| | | source_path = os.path.join(file_path, file) |
| | | source_excel = load_workbook(source_path) |
| | | |
| | | # 动态获取工作表 |
| | |
| | | template_sheets[name].cell(row=i, column=1).value = i - start_row + 1 |
| | | |
| | | timestamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S') |
| | | output_path = os.path.join(EXCEL_FILES_PATH, f'{timestamp}.xlsx') |
| | | random_string = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(5)) |
| | | file_name = f'{random_string}_{timestamp}' |
| | | output_path = os.path.join(print_path, f'{file_name}.xlsx') |
| | | template_excel.save(output_path) |
| | | template_excel.close() |
| | | |
| | | return True |
| | | delete_file_after_delay(output_path, 5) |
| | | |
| | | try: |
| | | for filename in os.listdir(file_path): |
| | | file_path_full = os.path.join(file_path, filename) |
| | | if os.path.isfile(file_path_full) or os.path.islink(file_path_full): |
| | | os.unlink(file_path_full) |
| | | elif os.path.isdir(file_path_full): |
| | | shutil.rmtree(file_path_full) |
| | | os.rmdir(file_path) |
| | | except Exception as e: |
| | | print(f"删除文件时发生错误: {e}") |
| | | |
| | | return file_name |
| | | except Exception as e: |
| | | print(f"读取数据发生错误: {e}") |
| | | return False |
| | | return None |