-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_code_format.py
More file actions
191 lines (167 loc) · 6.34 KB
/
Copy pathfix_code_format.py
File metadata and controls
191 lines (167 loc) · 6.34 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Fix code formatting in existing reports without overwriting other content."""
import sys
from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
sys.stdout.reconfigure(encoding='utf-8')
REPORTS = [
"C:/Users/50469/python-learning/实验七_Vue3待办事项应用_实验报告.docx",
"C:/Users/50469/python-learning/实验八_Vue3响应式商城_实验报告.docx",
]
def has_code_formatting(paragraph):
"""Check if a paragraph already has the gray shading."""
pPr = paragraph._element.find(qn('w:pPr'))
if pPr is not None:
shd = pPr.find(qn('w:shd'))
if shd is not None and shd.get(qn('w:fill')) == 'F2F2F2':
return True
return False
def apply_code_formatting(paragraph):
"""Apply code block shading and border."""
pPr = paragraph._element.get_or_add_pPr()
# Shading
shd_elem = pPr.find(qn('w:shd'))
if shd_elem is None:
shd_elem = OxmlElement('w:shd')
pPr.append(shd_elem)
shd_elem.set(qn('w:val'), 'clear')
shd_elem.set(qn('w:fill'), 'F2F2F2')
# Border
existing_bdr = pPr.find(qn('w:pBdr'))
if existing_bdr is None:
pBdr = OxmlElement('w:pBdr')
pPr.append(pBdr)
else:
pBdr = existing_bdr
for side in ('top', 'left', 'bottom', 'right'):
# Check if border side already exists
found = False
for child in pBdr:
if child.tag == qn(f'w:{side}'):
found = True
break
if not found:
border = OxmlElement(f'w:{side}')
border.set(qn('w:val'), 'single')
border.set(qn('w:sz'), '4')
border.set(qn('w:space'), '4' if side in ('left', 'right') else '1')
border.set(qn('w:color'), 'auto')
pBdr.append(border)
def is_code_line(text):
"""Detect if a trimmed line looks like code."""
stripped = text.strip()
if not stripped:
return False
# HTML / template tags
if stripped.startswith('<') and '>' in stripped:
return True
if stripped.startswith('</'):
return True
# JS/TS keywords and patterns
js_starters = (
'import ', 'export ', 'const ', 'let ', 'var ',
'function ', 'async ', 'await ', 'return ',
'if ', 'else ', 'else if ',
'try ', 'catch ', 'finally ',
'throw ', 'new ', 'typeof ', 'delete ',
'switch ', 'case ', 'break ', 'continue ',
'for ', 'while ', 'do ', 'in ',
'class ', 'extends ', 'interface ',
'default ', 'from ', 'yield ',
'import', 'export',
)
if any(stripped.startswith(p) for p in js_starters):
return True
# Vue directives / template expressions
if stripped.startswith('{{') or stripped.startswith('v-'):
return True
if ':key=' in stripped or ':src=' in stripped or ':class=' in stripped:
return True
if 'v-if' in stripped or 'v-else' in stripped or 'v-for' in stripped or 'v-model' in stripped:
return True
if 'v-bind' in stripped or 'v-on' in stripped or 'v-show' in stripped:
return True
# CSS / style patterns
if stripped.startswith('@media'):
return True
if stripped.startswith('* {'):
return True
if stripped.startswith('.') and ('{' in stripped or stripped.endswith(',') or stripped.endswith(' {')):
return True
if text.lstrip().startswith('.') and ':' in text and text.rstrip().endswith(';'):
return True
if 'flex' in stripped and ';' in stripped:
return True
if stripped.endswith(';') and ':' in stripped and not any(c.isascii() and c.isalpha() for c in stripped):
return True
# JS object / array patterns
if stripped.startswith('{') or stripped.startswith('}'):
return True
if stripped.startswith('[') or stripped.startswith(']'):
return True
if stripped.startswith('(') or stripped.startswith(')'):
return True
if stripped.startswith('...'): # spread operator
return True
# Lines ending with specific code patterns
if stripped in ('});', '})', ');', ']', '};', '},', '],', ')}', '))'):
return True
if stripped.endswith('),') or stripped.endswith(',') and not stripped.endswith('。'):
return True
# Property access / method call
if '.value' in stripped or '.text' in stripped:
return True
if '.push(' in stripped or '.filter(' in stripped or '.map(' in stripped:
return True
if '.find(' in stripped or '.splice(' in stripped or '.findIndex(' in stripped:
return True
if '.length' in stripped or '.trim()' in stripped:
return True
if '.getItem(' in stripped or '.setItem(' in stripped:
return True
if 'document.' in stripped or 'window.' in stripped:
return True
if 'console.' in stripped:
return True
if 'Promise.' in stripped or 'setTimeout' in stripped:
return True
if ' => ' in stripped or '=>' in stripped:
return True
if '`${' in stripped:
return True
# Arrow function / function call
if stripped.startswith('}') and stripped.endswith(')'):
return True
# JS object property (key: value pattern in code context)
if ': ' in stripped and stripped.endswith(',') and not stripped.endswith('。'):
parts = stripped.split(': ', 1)
key_part = parts[0].strip()
# Key is typically a simple identifier or string
if key_part and (key_part.isidentifier() or key_part.startswith("'") or key_part.startswith('"')):
return True
if key_part.startswith('...'):
return True
# Pipe-style code patterns
if stripped.count('|') >= 2:
return True
return False
for path in REPORTS:
print(f"\nProcessing: {path}")
doc = Document(path)
cell = doc.tables[0].rows[3].cells[0]
fixed = 0
already = 0
for para in cell.paragraphs:
text = para.text.strip()
if not text:
continue
looks_like_code = is_code_line(text)
already_formatted = has_code_formatting(para)
if looks_like_code and not already_formatted:
apply_code_formatting(para)
fixed += 1
elif looks_like_code and already_formatted:
already += 1
doc.save(path)
print(f" Fixed: {fixed} code lines (already formatted: {already})")
print("\nDone! All existing reports updated.")