-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_alt_text.py
More file actions
182 lines (147 loc) · 6.9 KB
/
Copy pathadd_alt_text.py
File metadata and controls
182 lines (147 loc) · 6.9 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
from bs4 import BeautifulSoup
import os
def generate_alt_text(src, page_name=""):
src_lower = src.lower()
page_lower = page_name.lower()
# Logo
if "logo" in src_lower:
return "Builtline Construction Logo - Trusted Builders in Bangalore"
# Hero/Banner
if any(x in src_lower for x in ["hero", "banner", "main", "bg", "background"]):
return "Best Construction Company in Bangalore - Builtline Construction"
# About
if "about" in src_lower:
return "Builtline Construction Team - Experienced Builders in Bangalore"
# Residential
if "residential" in src_lower:
return "Residential Building Construction in Bangalore - Builtline"
# Commercial
if "commercial" in src_lower:
return "Commercial Building Construction in Bangalore - Builtline"
# Renovation
if "renovation" in src_lower:
return "Home Renovation Services in Bangalore - Builtline Construction"
# Structural
if "structural" in src_lower:
return "Structural Works and Construction in Bangalore - Builtline"
# Planning
if "planning" in src_lower or "plan" in src_lower:
return "Construction Project Planning in Bangalore - Builtline"
# Quality/Safety
if "quality" in src_lower or "safety" in src_lower:
return "Quality and Safety Standards in Construction - Builtline"
# Sustainable
if "sustainable" in src_lower or "eco" in src_lower:
return "Sustainable Construction Solutions in Bangalore - Builtline"
# G+2
if any(x in src_lower for x in ["g2", "gplus2", "g-2", "g_2"]):
return "G+2 House Construction in Bangalore - Builtline Construction"
# G+1
if any(x in src_lower for x in ["g1", "gplus1", "g-1", "g_1"]):
return "G+1 House Construction in Bangalore - Builtline Construction"
# 30x40
if "30x40" in src_lower or "30-40" in src_lower:
return "30x40 House Construction in Bangalore - Builtline Construction"
# Checklist
if "checklist" in src_lower:
return "House Construction Checklist in Bangalore - Builtline"
# Turnkey
if "turnkey" in src_lower:
return "Turnkey Construction Company in Bangalore - Builtline"
# Project/Work
if any(x in src_lower for x in ["project", "work", "site", "construction"]):
return "Construction Project in Bangalore - Builtline Construction"
# Team/Staff
if any(x in src_lower for x in ["team", "staff", "worker", "engineer"]):
return "Builtline Construction Team - Professional Builders Bangalore"
# Area wise — check page name too
area_map = {
"malleshwaram": "Construction Company in Malleshwaram Bangalore",
"whitefield": "Construction Company in Whitefield Bangalore",
"indiranagar": "Construction Company in Indiranagar Bangalore",
"jayanagar": "Construction Company in Jayanagar Bangalore",
"hebbal": "Construction Company in Hebbal Bangalore",
"yelahanka": "Construction Company in Yelahanka Bangalore",
"yalahanka": "Construction Company in Yelahanka Bangalore",
"rajajinagar": "Construction Company in Rajajinagar Bangalore",
"basavanagudi": "Construction Company in Basavanagudi Bangalore",
"electroniccity": "Construction Company in Electronic City Bangalore",
"electronic": "Construction Company in Electronic City Bangalore",
"banashankari": "Construction Company in Banashankari Bangalore",
"koramangala": "Construction Company in Koramangala Bangalore",
"bannerghatta": "Construction Company in Bannerghatta Bangalore",
"banneragatta": "Construction Company in Bannerghatta Bangalore",
"mahadevpura": "Construction Company in Mahadevpura Bangalore",
"yeswanthpur": "Construction Company in Yeswanthpur Bangalore",
"yashwanthpur": "Construction Company in Yeswanthpur Bangalore",
"gangondanahalli": "Construction Company in Gangondanahalli Bangalore",
"basaveshwar": "Construction Company in Basaveshwar Nagar Bangalore",
"rrnagar": "Construction Company in RR Nagar Bangalore",
"kengeri": "Construction Company in Kengeri Bangalore",
"vidyapeeta": "Construction Company in Vidyapeeta Bangalore",
"hemmigepura": "Construction Company in Hemmigepura Bangalore",
"sahakaranagar": "Construction Company in Sahakara Nagar Bangalore",
}
for area, alt in area_map.items():
if area in src_lower or area in page_lower:
return alt + " - Builtline Construction"
# Default fallback
return "Construction Services in Bangalore - Builtline Construction"
def process_html_files():
# Get current directory
current_dir = os.getcwd()
# Find all HTML files
html_files = []
for root, dirs, files in os.walk(current_dir):
# Skip hidden folders
dirs[:] = [d for d in dirs if not d.startswith('.')]
for file in files:
if file.endswith('.html') or file.endswith('.htm'):
html_files.append(os.path.join(root, file))
if not html_files:
print("No HTML files found in current directory!")
return
print(f"Found {len(html_files)} HTML files")
print("-" * 50)
total_updated = 0
total_images = 0
for filepath in html_files:
filename = os.path.basename(filepath)
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
soup = BeautifulSoup(content, 'html.parser')
images = soup.find_all('img')
updated_count = 0
for img in images:
# Only update empty or missing alt tags
current_alt = img.get('alt', None)
if current_alt is None or current_alt.strip() == '':
src = img.get('src', '')
alt_text = generate_alt_text(src, filename)
img['alt'] = alt_text
updated_count += 1
total_images += 1
if updated_count > 0:
# Save updated file
with open(filepath, 'w', encoding='utf-8') as f:
f.write(str(soup))
print(f"✅ {filename} — {updated_count} images updated")
total_updated += 1
else:
print(f"⏭️ {filename} — No empty alt tags found")
except Exception as e:
print(f"❌ Error in {filename}: {e}")
print("-" * 50)
print(f"✅ DONE! Updated {total_images} images across {total_updated} files!")
print()
print("Next steps:")
print("1. Upload all updated HTML files to your server")
print("2. Run PageSpeed test again to see improvement")
print("3. Check Accessibility score — should improve!")
if __name__ == "__main__":
print("=" * 50)
print("BUILTLINE CONSTRUCTION - AUTO ALT TEXT ADDER")
print("=" * 50)
print()
process_html_files()