ahmedj-turing commited on
Commit
72244ee
·
verified ·
1 Parent(s): 40c9077

Upload diagnose_fleet_v4.py

Browse files
Files changed (1) hide show
  1. diagnose_fleet_v4.py +104 -0
diagnose_fleet_v4.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, zipfile, sys
2
+ import xml.etree.ElementTree as ET
3
+
4
+ def _local(tag): return tag.rsplit('}', 1)[-1]
5
+
6
+ def diagnose():
7
+ path = os.path.expanduser('~/fleet_operations_Q3.xlsx')
8
+ if not os.path.exists(path):
9
+ print("FILE NOT FOUND at", path)
10
+ return
11
+
12
+ print("--- Diagnosing:", path, "---")
13
+ try:
14
+ z = zipfile.ZipFile(path)
15
+ except Exception as e:
16
+ print("Not a valid ZIP/XLSX file:", e)
17
+ return
18
+
19
+ names = z.namelist()
20
+ def rd(n): return z.read(n).decode('utf-8', 'replace')
21
+
22
+ # Styles
23
+ num_fmts = {}
24
+ cellxfs = []
25
+ fills = []
26
+ if 'xl/styles.xml' in names:
27
+ sroot = ET.fromstring(rd('xl/styles.xml'))
28
+ for el in sroot:
29
+ if _local(el.tag) == 'numFmts':
30
+ for nf in el:
31
+ num_fmts[int(nf.get('numFmtId'))] = nf.get('formatCode', '')
32
+ elif _local(el.tag) == 'fills':
33
+ for f in el:
34
+ has_color = False
35
+ for pf in f:
36
+ if _local(pf.tag) == 'patternFill' and pf.get('patternType') != 'none':
37
+ has_color = True
38
+ fills.append(has_color)
39
+ elif _local(el.tag) == 'cellXfs':
40
+ for xf in el:
41
+ if _local(xf.tag) == 'xf':
42
+ cellxfs.append({
43
+ 'numFmtId': int(xf.get('numFmtId', '0')),
44
+ 'fillId': int(xf.get('fillId', '0'))
45
+ })
46
+ print(f"Custom Number Formats: {num_fmts}")
47
+ print(f"Total fills parsed: {len(fills)}")
48
+
49
+ # Sheets
50
+ wb = ET.fromstring(rd('xl/workbook.xml'))
51
+ rels = ET.fromstring(rd('xl/_rels/workbook.xml.rels'))
52
+ rel_map = {rel.get('Id'): rel.get('Target') for rel in rels}
53
+
54
+ for sheet in wb.iter():
55
+ if _local(sheet.tag) == 'sheet':
56
+ tgt = rel_map.get(sheet.get('{http://schemas.openxmlformats.org/officeDocument/2006/relationships}id'))
57
+ tgt = tgt.lstrip('/') if tgt.startswith('/') else 'xl/' + tgt
58
+ print(f"\n--- Scanning Sheet: {sheet.get('name')} ---")
59
+
60
+ sroot = ET.fromstring(rd(tgt))
61
+
62
+ # Panes
63
+ for pane in sroot.iter():
64
+ if _local(pane.tag) == 'pane':
65
+ print(f"Pane: ySplit={pane.get('ySplit')}, state={pane.get('state')}")
66
+
67
+ # Validations
68
+ print("Data Validations:")
69
+ found_dv = False
70
+ for dv in sroot.iter():
71
+ if 'dataValidation' in _local(dv.tag):
72
+ found_dv = True
73
+ sqref = dv.get('sqref', '')
74
+ if not sqref:
75
+ for ch in dv.iter():
76
+ if 'sqref' in _local(ch.tag).lower() and ch.text: sqref = ch.text
77
+ f1 = ''.join([ch.text for ch in dv.iter() if _local(ch.tag) in ('formula1', 'f1') and ch.text])
78
+ print(f" sqref: {sqref} | formula: {f1}")
79
+ if not found_dv: print(" None found.")
80
+
81
+ # Headers (Row 1)
82
+ print("\nHeaders (Row 1) Fill Check:")
83
+ for row in sroot.iter():
84
+ if _local(row.tag) == 'row' and row.get('r') == '1':
85
+ for c in row:
86
+ if _local(c.tag) == 'c' and c.get('r') in ('A1', 'J1', 'K1', 'L1', 'M1'):
87
+ s_idx = int(c.get('s', '0'))
88
+ has_fill = fills[cellxfs[s_idx]['fillId']] if s_idx < len(cellxfs) else False
89
+ print(f" {c.get('r')}: styleIdx={s_idx}, has_fill={has_fill}")
90
+
91
+ # Formulas and Formats (Row 2 and Row 151)
92
+ print("\nFormulas & Currency Check (Row 2 & 151):")
93
+ for row in sroot.iter():
94
+ if _local(row.tag) == 'row' and row.get('r') in ('2', '151'):
95
+ for c in row:
96
+ if _local(c.tag) == 'c' and c.get('r') in ('K2', 'L2', 'M2', 'K151', 'L151', 'M151'):
97
+ s_idx = int(c.get('s', '0'))
98
+ num_fmt = cellxfs[s_idx]['numFmtId'] if s_idx < len(cellxfs) else 0
99
+ f_txt = ''.join([ch.text for ch in c if _local(ch.tag) == 'f' and ch.text])
100
+ code = num_fmts.get(num_fmt, 'builtin')
101
+ print(f" {c.get('r')}: formatID={num_fmt} ({code}), formula={f_txt}")
102
+
103
+ if __name__ == '__main__':
104
+ diagnose()