Ver código fonte

Merge branch 'master' of http://192.168.5.5:10010/r/rag-gateway

zhaoqingang 1 ano atrás
pai
commit
945a5ad142
1 arquivos alterados com 36 adições e 44 exclusões
  1. 36 44
      app/utils/excelmerge/conformity.py

+ 36 - 44
app/utils/excelmerge/conformity.py

@@ -3,6 +3,22 @@ from datetime import datetime
 import os
 
 
+def clear_blank_rows(sheet):
+    last_row = sheet.max_row
+    for row in range(last_row, 1, -1):
+        if all(cell.value is None or cell.value == '' for cell in sheet[row]):
+            sheet.delete_rows(row)
+
+
+def copy_data(source_sheet, target_sheet, start_row):
+    for row in range(start_row, source_sheet.max_row + 1):
+        a_cell_value = source_sheet.cell(row=row, column=1).value
+        if isinstance(a_cell_value, (int, float)) and any(
+                source_sheet.cell(row=row, column=col).value for col in range(4, source_sheet.max_column + 1)):
+            target_sheet.append(
+                [source_sheet.cell(row=row, column=col).value for col in range(1, source_sheet.max_column + 1)])
+
+
 def run_conformity():
     try:
         # 加载模板文件
@@ -10,14 +26,8 @@ def run_conformity():
         template_excel = load_workbook(template_path)
         EXCEL_FILES_PATH = os.path.join('data', 'output')
 
-        # 获取工作表
-        sheet1 = template_excel['技术监督工作统计表']
-        sheet2 = template_excel['技术监督告(预)警单统计表']
-        sheet3 = template_excel['投产前技术监督报告统计表']
-        sheet4 = template_excel['技术监督细则完善建议']
-        sheet5 = template_excel['典型经验交流']
+        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('~$')]
 
@@ -25,47 +35,29 @@ def run_conformity():
             source_path = os.path.join(source_folder, file)
             source_excel = load_workbook(source_path)
 
-            # 获取源工作表
-            ssheet1 = source_excel['技术监督工作统计表']
-            ssheet2 = source_excel['技术监督告(预)警单统计表']
-            ssheet3 = source_excel['投产前技术监督报告统计表']
-            ssheet4 = source_excel['技术监督细则完善建议']
-            ssheet5 = source_excel['典型经验交流']
-
-            # 清除无效空白行
-            for ssheet in [ssheet1, ssheet2, ssheet3, ssheet4, ssheet5]:
-                last_row = ssheet.max_row
-                if last_row > 1:
-                    for row in range(last_row, 1, -1):
-                        if all(ssheet.cell(row=row, column=col).value is None or ssheet.cell(row=row,
-                                                                                             column=col).value == ''
-                               for col in range(1, ssheet.max_column + 1)):
-                            ssheet.delete_rows(row)
-
-            # 复制数据
-            for (ssheet, tsheet, start_point) in [
-                (ssheet1, sheet1, 4),
-                (ssheet2, sheet2, 3),
-                (ssheet3, sheet3, 3),
-                (ssheet4, sheet4, 4),
-                (ssheet5, sheet5, 3),
-            ]:
-                for row in range(start_point, ssheet.max_row + 1):
-                    a_cell_value = ssheet.cell(row=row, column=1).value
-                    if isinstance(a_cell_value, (int, float)) and any(
-                            ssheet.cell(row=row, column=col).value for col in range(2, ssheet.max_column + 1)):
-                        tsheet.append(
-                            [ssheet.cell(row=row, column=col).value for col in range(1, ssheet.max_column + 1)])
+            # 动态获取工作表
+            source_sheets = {sheet.title: sheet for sheet in source_excel}
+
+            for name in template_sheets:
+                if name in source_sheets:
+                    clear_blank_rows(source_sheets[name])
+
+            for name, start_row in [('技术监督工作统计表', 4), ('技术监督告(预)警单统计表', 3),
+                                    ('投产前技术监督报告统计表', 3), ('技术监督细则完善建议', 4),
+                                    ('典型经验交流', 3)]:
+                if name in source_sheets and name in template_sheets:
+                    copy_data(source_sheets[name], template_sheets[name], start_row)
 
             source_excel.close()
 
-        # 在目标工作表中添加序号
-        for tsheet, start_point in [(sheet1, 4), (sheet2, 3), (sheet3, 3), (sheet4, 4), (sheet5, 3)]:
-            last_row = tsheet.max_row
-            for i in range(start_point, last_row + 1):
-                tsheet.cell(row=i, column=1).value = i - start_point + 1
+        for name, start_row in [('技术监督工作统计表', 4), ('技术监督告(预)警单统计表', 3),
+                                ('投产前技术监督报告统计表', 3), ('技术监督细则完善建议', 4),
+                                ('典型经验交流', 3)]:
+            if name in template_sheets:
+                last_row = template_sheets[name].max_row
+                for i in range(start_row, last_row + 1):
+                    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')
         template_excel.save(output_path)