NHÀ BẠN ĐANG XUỐNG CẤP NHƯNG VẪN Ở – 90% TRƯỜNG HỢP CHỈ CẦN CHẬM MỘT THỜI GIAN LÀ CHI PHÍ SẼ TĂNG GẤP ĐÔI

Ngày đăng: 13-07-2026

from bs4 import BeautifulSoup
from pathlib import Path

src = Path("/mnt/data/Văn bản đã dán (1)(44).txt")
out = Path("/mnt/data/Sua-nha-dung-thoi-diem-bo-cuc-chuan.html")

soup = BeautifulSoup(src.read_text(encoding="utf-8"), "html.parser")

# Xóa span lồng nhau để tránh sai font
for span in soup.find_all("span"):
    span.unwrap()

# Xóa khối rỗng
for tag in list(soup.find_all(["p", "h1", "h2", "h3"])):
    if not tag.get_text(" ", strip=True) and not tag.find("img"):
        tag.decompose()

# Chuyển đúng cấp các tiêu đề lớn đang bị để dạng đoạn văn
main_titles = {
    "Những Dấu Hiệu Xuống Cấp Không Nên Bỏ Qua",
    "Vì Sao Chi Phí Sửa Nhà Tăng Theo Thời Gian?",
}
for p in list(soup.find_all("p")):
    if p.get_text(" ", strip=True) in main_titles:
        p.name = "h1"

# Style chữ theo đúng mẫu cũ
for p in soup.find_all("p"):
    p["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:16px;line-height:1.7;text-align:justify;"
        "margin:0 0 14px;"
    )

for h1 in soup.find_all("h1"):
    h1["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:24px;line-height:1.35;font-weight:700;"
        "text-align:center;color:#0b4f8a;margin:34px 0 18px;"
    )

for h2 in soup.find_all("h2"):
    h2["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:20px;line-height:1.4;font-weight:700;"
        "text-align:left;color:#173f66;margin:26px 0 12px;"
    )

for h3 in soup.find_all("h3"):
    h3["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:17px;line-height:1.45;font-weight:700;"
        "text-align:left;color:#1f2937;margin:20px 0 8px;"
    )

for ul in soup.find_all("ul"):
    ul["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:16px;line-height:1.7;"
        "margin:10px 0 18px 24px;padding-left:20px;"
    )

for li in soup.find_all("li"):
    li["style"] = "margin-bottom:6px;"

# Khung Nội Dung Bài Viết
toc = next(
    (h for h in soup.find_all("h1")
     if h.get_text(" ", strip=True).lower() == "nội dung bài viết"),
    None
)

if toc:
    toc_list = toc.find_next_sibling("ul")
    toc_box = soup.new_tag("div")
    toc_box["style"] = (
        "margin:24px 0 30px;border:1px solid #76a9d2;"
        "border-radius:10px;overflow:hidden;background:#eaf5ff;"
        "box-shadow:0 3px 10px rgba(0,77,128,.10);"
    )
    toc.insert_before(toc_box)
    toc_box.append(toc.extract())

    if toc_list:
        toc_box.append(toc_list.extract())
        toc_list["style"] = (
            "font-family:'Times New Roman',Times,serif;"
            "font-size:16px;line-height:1.7;"
            "margin:0;padding:16px 34px 18px 48px;"
            "background:#eaf5ff;"
        )

    toc["style"] = (
        "font-family:'Times New Roman',Times,serif;"
        "font-size:22px;font-weight:700;text-align:center;"
        "color:#0d4977;margin:0;padding:14px 16px;"
        "background:#cfe8fb;"
    )

# Khung lưu ý
for blockquote in list(soup.find_all("blockquote")):
    note = blockquote.get_text(" ", strip=True)
    if note.lower().startswith("lưu ý:"):
        note = note[6:].strip()

    note_box = soup.new_tag("div")
    note_box["style"] = (
        "display:block !important;width:auto !important;"
        "margin:18px 0 26px !important;padding:14px 18px !important;"
        "background-color:#eaf5ff !important;"
        "border:1px solid #79acd3 !important;"
        "border-left:5px solid #2c79b8 !important;"
        "border-radius:8px !important;"
        "box-shadow:0 2px 8px rgba(0,77,128,.08) !important;"
        "font-family:'Times New Roman',Times,serif !important;"
        "font-size:16px !important;line-height:1.7 !important;"
        "color:#1f2d3d !important;text-align:justify !important;"
        "box-sizing:border-box !important;"
    )

    strong = soup.new_tag("strong")
    strong.string = "Lưu ý:"
    note_box.append(strong)
    note_box.append(" " + note)
    blockquote.replace_with(note_box)

# Style ảnh cơ bản
for img in soup.find_all("img"):
    img["style"] = (
        "display:block !important;max-width:100% !important;"
        "height:auto !important;margin:0 auto !important;"
        "border-radius:8px !important;"
    )

# Gom nhóm ảnh liền nhau: 2 ảnh ngang + 1 ảnh phía dưới
def get_image_groups(root):
    groups = []
    current = []

    for child in list(root.children):
        if getattr(child, "name", None) == "p" and child.find("img"):
            current.append(child)
        else:
            if current:
                groups.append(current)
                current = []

    if current:
        groups.append(current)

    return groups

for group in get_image_groups(soup):
    if len(group) < 2:
        continue

    gallery = soup.new_tag("table")
    gallery["style"] = (
        "width:100% !important;border-collapse:separate !important;"
        "border-spacing:12px 0 !important;margin:20px 0 24px !important;"
        "table-layout:fixed !important;background:transparent !important;"
    )

    tbody = soup.new_tag("tbody")
    row = soup.new_tag("tr")

    for p in group[:2]:
        td = soup.new_tag("td")
        td["style"] = (
            "width:50% !important;vertical-align:middle !important;"
            "text-align:center !important;padding:8px !important;"
            "border:1px solid #c9dceb !important;"
            "background:#f5f9fc !important;border-radius:10px !important;"
            "box-sizing:border-box !important;"
        )

        img = p.find("img")
        img.extract()
        img["style"] = (
            "display:block !important;width:100% !important;"
            "height:320px !important;object-fit:cover !important;"
            "margin:0 auto !important;border-radius:6px !important;"
        )

        td.append(img)
        row.append(td)

    tbody.append(row)
    gallery.append(tbody)
    group[0].insert_before(gallery)

    if len(group) >= 3:
        third_box = soup.new_tag("div")
        third_box["style"] = (
            "margin:0 0 24px !important;padding:8px !important;"
            "border:1px solid #c9dceb !important;"
            "background:#f5f9fc !important;border-radius:10px !important;"
            "box-sizing:border-box !important;text-align:center !important;"
        )

        third = group[2].find("img")
        third.extract()
        third["style"] = (
            "display:block !important;width:100% !important;"
            "height:auto !important;max-height:520px !important;"
            "object-fit:cover !important;margin:0 auto !important;"
            "border-radius:6px !important;"
        )

        third_box.append(third)
        gallery.insert_after(third_box)

    for p in group:
        p.decompose()

# Một khung ngoài duy nhất
container = soup.new_tag("div")
container["style"] = (
    "max-width:980px;margin:0 auto;padding:10px 16px 36px;"
    "font-family:'Times New Roman',Times,serif;color:#222;"
)

for child in list(soup.contents):
    if getattr(child, "name", None):
        container.append(child.extract())

soup.append(container)

out.write_text(str(soup), encoding="utf-8")
print(out)
 

Hoạt động khác