-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMerger-HTML.py
More file actions
92 lines (77 loc) · 3.44 KB
/
Copy pathMerger-HTML.py
File metadata and controls
92 lines (77 loc) · 3.44 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
from bs4 import BeautifulSoup
# Define your HTML files in order
html_files = [
'DS-1-250-AliBook.html',
'ds-251-500-AliBook.html',
'ds-501-750-AliBook.html',
'ds-750-1000-AliBook.html'
]
output_file = 'merged_output.html'
# Load the first file as base (it contains the proper structure, styles, etc.)
try:
with open(html_files[0], 'r', encoding='utf-8') as f:
merged_soup = BeautifulSoup(f.read(), 'html.parser')
except FileNotFoundError:
print(f"Error: First file {html_files[0]} not found!")
exit()
# Ensure proper Arabic/RTL attributes
if not merged_soup.html.has_attr('lang'):
merged_soup.html['lang'] = 'ar'
if not merged_soup.html.has_attr('dir'):
merged_soup.html['dir'] = 'rtl'
# Find or create the main .book container
book_div = merged_soup.find('div', class_='book')
if not book_div:
# Fallback: create it if missing
book_div = merged_soup.new_tag('div', attrs={'class': 'book'})
body = merged_soup.find('body')
if body:
body.append(book_div)
else:
new_body = merged_soup.new_tag('body')
new_body.append(book_div)
merged_soup.html.append(new_body)
# Keep track of the last <hr> or last hukm to avoid duplicate separators
for index, file_name in enumerate(html_files[1:], start=1): # Skip first file
try:
with open(file_name, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
print(f"Processing: {file_name}")
# Extract content from .book div
file_book = soup.find('div', class_='book')
content_to_add = []
if file_book:
# Get all children inside .book
for child in file_book.find_all(recursive=False):
# Skip title/subtitle/author section (only keep from first file)
if child.name == 'div' and any(cls in ['title', 'subtitle', 'author'] for cls in child.get('class', [])):
continue
if child.name == 'hr' and len(content_to_add) == 0: # Skip first hr in subsequent files
continue
content_to_add.append(child)
else:
# Fallback: take everything from body if no .book found
if soup.body:
content_to_add = soup.body.find_all(recursive=False)
if content_to_add:
# Add a separator comment
separator = BeautifulSoup(f"\n<!-- ==================== Content from {file_name} ==================== -->\n", "html.parser")
book_div.append(separator)
# Add the content
for element in content_to_add:
book_div.append(element)
except FileNotFoundError:
print(f"Warning: {file_name} not found, skipped.")
except Exception as e:
print(f"Error processing {file_name}: {e}")
# Clean up any duplicate titles or meta tags (optional safety)
for tag in merged_soup.head.find_all('title')[1:]:
tag.decompose()
for tag in merged_soup.head.find_all('meta'):
if tag.get('charset') or tag.get('http-equiv'):
continue # keep charset
# Save with nice formatting
with open(output_file, 'w', encoding='utf-8') as f:
f.write(merged_soup.prettify())
print(f"\nSuccess! Merged file saved as: {output_file}")
print("The original styling, RTL direction, and .book layout have been preserved.")