zhangxiao
2024-10-16 f192a7a302373ae931104d6cb44076fe694da470
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import xlwings as xw
from datetime import datetime
import os
import shutil
 
 
def run_conformity():
 
    source_folder = os.path.join('data', 'source')
    template_path = os.path.join('app', 'utils', 'excelmerge', '国网上海电力整合模版.xlsx')
    EXCEL_FILES_PATH = os.path.join('data', 'output')
 
    app = xw.App(visible=False)
    templateExcel = xw.Book(template_path)
 
    sheet1 = templateExcel.sheets['技术监督工作统计表']
    sheet2 = templateExcel.sheets['技术监督告(预)警单统计表']
    sheet3 = templateExcel.sheets['投产前技术监督报告统计表']
    sheet4 = templateExcel.sheets['技术监督细则完善建议']
    sheet5 = templateExcel.sheets['典型经验交流']
 
    source_files = [f for f in os.listdir(source_folder) if f.endswith('.xlsx') and not f.startswith('~$')]
 
    for file in source_files:
        sourceExcel = xw.Book(os.path.join(source_folder, file))
        ssheet1 = sourceExcel.sheets['技术监督工作统计表']
        ssheet2 = sourceExcel.sheets['技术监督告(预)警单统计表']
        ssheet3 = sourceExcel.sheets['投产前技术监督报告统计表']
        ssheet4 = sourceExcel.sheets['技术监督细则完善建议']
        ssheet5 = sourceExcel.sheets['典型经验交流']
        for ssheet in [ssheet1, ssheet2, ssheet3, ssheet4, ssheet5]:
            last_row_value = ssheet.range(f'A{ssheet.used_range.rows.count}').value
            if isinstance(last_row_value, str):
                ssheet.range(f'A{ssheet.used_range.rows.count}:AA{ssheet.used_range.rows.count}').delete()
        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.used_range.rows.count + 1):
                a_cell_value = ssheet.range(f'A{row}').value
                if not isinstance(a_cell_value, (int, float)):
                    continue
                if not all(cell.value is None or cell.value == '' for cell in ssheet.range(f'B{row}:AA{row}')):
                    tsheet.range(f'B{tsheet.used_range.rows.count + 1}').value = ssheet.range(f'B{row}:AA{row}').value
        sourceExcel.close()
    for tsheet, start_point in [(sheet1, 4), (sheet2, 3), (sheet3, 3), (sheet4, 4), (sheet5, 3)]:
        last_row = tsheet.used_range.rows.count
        number = 1
        for i in range(start_point, last_row + 1):
            tsheet.range(f'A{i}').value = number
            number += 1
 
    timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    output_path = os.path.join(EXCEL_FILES_PATH, f'{timestamp}.xlsx')
    templateExcel.save(output_path)
    templateExcel.close()
    app.quit()
 
    return timestamp