conformity.py 3.6 KB

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