conformity.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os
  2. import random
  3. import shutil
  4. import string
  5. import threading
  6. import time
  7. from datetime import datetime
  8. from openpyxl import load_workbook
  9. from Log import logger
  10. def clear_blank_rows(sheet):
  11. last_row = sheet.max_row
  12. for row in range(last_row, 1, -1):
  13. if all(cell.value is None or cell.value == '' for cell in sheet[row]):
  14. sheet.delete_rows(row)
  15. def copy_data(source_sheet, target_sheet, start_row):
  16. for row in range(start_row, source_sheet.max_row + 1):
  17. a_cell_value = source_sheet.cell(row=row, column=1).value
  18. if isinstance(a_cell_value, (int, float)) and any(
  19. source_sheet.cell(row=row, column=col).value for col in range(4, source_sheet.max_column + 1)):
  20. target_sheet.append(
  21. [source_sheet.cell(row=row, column=col).value for col in range(1, source_sheet.max_column + 1)])
  22. def delete_file_after_delay(file_path, delay_minutes):
  23. delay_seconds = delay_minutes * 60
  24. def delete_file():
  25. time.sleep(delay_seconds)
  26. try:
  27. if os.path.exists(file_path):
  28. os.remove(file_path)
  29. except Exception as e:
  30. logger.error(f"定时删除Excel文件时发生错误: {e}")
  31. threading.Thread(target=delete_file).start()
  32. def run_conformity(file_path, print_path):
  33. try:
  34. # 加载模板文件
  35. template_path = os.path.join('app', 'utils', 'excelmerge', '国网上海电力整合模版.xlsx')
  36. template_excel = load_workbook(template_path)
  37. template_sheets = {sheet.title: sheet for sheet in template_excel}
  38. source_files = [f for f in os.listdir(file_path) if f.endswith('.xlsx') and not f.startswith('~$')]
  39. for file in source_files:
  40. source_path = os.path.join(file_path, file)
  41. source_excel = load_workbook(source_path)
  42. # 动态获取工作表
  43. source_sheets = {sheet.title: sheet for sheet in source_excel}
  44. for name in template_sheets:
  45. if name in source_sheets:
  46. clear_blank_rows(source_sheets[name])
  47. for name, start_row in [('技术监督工作统计表', 4), ('技术监督告(预)警单统计表', 3),
  48. ('投产前技术监督报告统计表', 3), ('技术监督细则完善建议', 4),
  49. ('典型经验交流', 3)]:
  50. if name in source_sheets and name in template_sheets:
  51. copy_data(source_sheets[name], template_sheets[name], start_row)
  52. source_excel.close()
  53. for name, start_row in [('技术监督工作统计表', 4), ('技术监督告(预)警单统计表', 3),
  54. ('投产前技术监督报告统计表', 3), ('技术监督细则完善建议', 4),
  55. ('典型经验交流', 3)]:
  56. if name in template_sheets:
  57. last_row = template_sheets[name].max_row
  58. for i in range(start_row, last_row + 1):
  59. template_sheets[name].cell(row=i, column=1).value = i - start_row + 1
  60. timestamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
  61. random_string = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(5))
  62. file_name = f'{random_string}_{timestamp}'
  63. output_path = os.path.join(print_path, f'{file_name}.xlsx')
  64. template_excel.save(output_path)
  65. template_excel.close()
  66. delete_file_after_delay(output_path, 5)
  67. try:
  68. for filename in os.listdir(file_path):
  69. file_path_full = os.path.join(file_path, filename)
  70. if os.path.isfile(file_path_full) or os.path.islink(file_path_full):
  71. os.unlink(file_path_full)
  72. elif os.path.isdir(file_path_full):
  73. shutil.rmtree(file_path_full)
  74. os.rmdir(file_path)
  75. except Exception as e:
  76. print(f"删除文件时发生错误: {e}")
  77. return file_name
  78. except Exception as e:
  79. print(f"读取数据发生错误: {e}")
  80. return None