From 72a8a0a1ad6b79b8e9fb2facef121f9b5d584666 Mon Sep 17 00:00:00 2001
From: xuyonghao <898441624@qq.com>
Date: 星期六, 08 二月 2025 10:56:30 +0800
Subject: [PATCH] 报表合并历史记录功能

---
 app/utils/excelmerge/conformity.py |  129 +++++++++++++++++++++++--------------------
 1 files changed, 69 insertions(+), 60 deletions(-)

diff --git a/app/utils/excelmerge/conformity.py b/app/utils/excelmerge/conformity.py
index 7276d06..0bf9f98 100644
--- a/app/utils/excelmerge/conformity.py
+++ b/app/utils/excelmerge/conformity.py
@@ -1,73 +1,82 @@
-from openpyxl import load_workbook
-from openpyxl.utils import get_column_letter
-from datetime import datetime
 import os
+import random
+import shutil
+import string
+
+from datetime import datetime
+from openpyxl import load_workbook
 
 
-def run_conformity():
-    # 鍔犺浇妯℃澘鏂囦欢
-    template_path = os.path.join('app', 'utils', 'excelmerge', '鍥界綉涓婃捣鐢靛姏鏁村悎妯$増.xlsx')
-    template_excel = load_workbook(template_path)
-    EXCEL_FILES_PATH = os.path.join('data', 'output')
+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)
 
-    # 鑾峰彇宸ヤ綔琛�
-    sheet1 = template_excel['鎶�鏈洃鐫e伐浣滅粺璁¤〃']
-    sheet2 = template_excel['鎶�鏈洃鐫e憡锛堥锛夎鍗曠粺璁¤〃']
-    sheet3 = template_excel['鎶曚骇鍓嶆妧鏈洃鐫f姤鍛婄粺璁¤〃']
-    sheet4 = template_excel['鎶�鏈洃鐫g粏鍒欏畬鍠勫缓璁�']
-    sheet5 = 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('~$')]
+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)])
 
-    for file in source_files:
-        source_path = os.path.join(source_folder, file)
-        source_excel = load_workbook(source_path)
 
-        # 鑾峰彇婧愬伐浣滆〃
-        ssheet1 = source_excel['鎶�鏈洃鐫e伐浣滅粺璁¤〃']
-        ssheet2 = source_excel['鎶�鏈洃鐫e憡锛堥锛夎鍗曠粺璁¤〃']
-        ssheet3 = source_excel['鎶曚骇鍓嶆妧鏈洃鐫f姤鍛婄粺璁¤〃']
-        ssheet4 = source_excel['鎶�鏈洃鐫g粏鍒欏畬鍠勫缓璁�']
-        ssheet5 = source_excel['鍏稿瀷缁忛獙浜ゆ祦']
+def run_conformity(file_path, print_path):
+    try:
+        # 鍔犺浇妯℃澘鏂囦欢
+        template_path = os.path.join('app', 'utils', 'excelmerge', '鍥界綉涓婃捣鐢靛姏鏁村悎妯$増.xlsx')
+        template_excel = load_workbook(template_path)
+        template_sheets = {sheet.title: sheet for sheet in template_excel}
+        source_files = [f for f in os.listdir(file_path) if f.endswith('.xlsx') and not f.startswith('~$')]
 
-        # 娓呴櫎鏃犳晥绌虹櫧琛�
-        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 file in source_files:
+            source_path = os.path.join(file_path, file)
+            source_excel = load_workbook(source_path)
 
-        # 澶嶅埗鏁版嵁
-        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}
 
-        source_excel.close()
+            for name in template_sheets:
+                if name in source_sheets:
+                    clear_blank_rows(source_sheets[name])
 
-    # 鍦ㄧ洰鏍囧伐浣滆〃涓坊鍔犲簭鍙�
-    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 [('鎶�鏈洃鐫e伐浣滅粺璁¤〃', 4), ('鎶�鏈洃鐫e憡锛堥锛夎鍗曠粺璁¤〃', 3),
+                                    ('鎶曚骇鍓嶆妧鏈洃鐫f姤鍛婄粺璁¤〃', 3), ('鎶�鏈洃鐫g粏鍒欏畬鍠勫缓璁�', 4),
+                                    ('鍏稿瀷缁忛獙浜ゆ祦', 3)]:
+                if name in source_sheets and name in template_sheets:
+                    copy_data(source_sheets[name], template_sheets[name], start_row)
 
-    # 淇濆瓨鏂囦欢
-    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)
-    template_excel.close()
+            source_excel.close()
 
-    return timestamp
+        for name, start_row in [('鎶�鏈洃鐫e伐浣滅粺璁¤〃', 4), ('鎶�鏈洃鐫e憡锛堥锛夎鍗曠粺璁¤〃', 3),
+                                ('鎶曚骇鍓嶆妧鏈洃鐫f姤鍛婄粺璁¤〃', 3), ('鎶�鏈洃鐫g粏鍒欏畬鍠勫缓璁�', 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')
+        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()
+
+        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 None
\ No newline at end of file

--
Gitblit v1.8.0