conformity.py 4.0 KB

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