# -*- coding: utf-8 -*-
r"""B179: trích toàn bộ text + bảng của DOCX 'THÁNG 10 - TUẦN 3. 14.10.2026.docx' (KPH tuần 3 tháng 10)."""
import io, os, shutil
from docx import Document

SRC = r'C:\Users\admin\AppData\Local\hermes\cache\documents\doc_b568ba2680ac_THÁNG 10 - TUẦN 3. 14.10.2026.docx'
RA = r'C:\Claw 2\QC\_kph_tuan3_1010'
os.makedirs(RA, exist_ok=True)
DST = os.path.join(RA, 'THANG10_TUAN3_14102026.docx')
P = os.path.join(RA, 'NOI_DUNG.txt')
if not os.path.exists(DST):
    shutil.copy2(SRC, DST)
d = Document(DST)
ra = []
for i, p in enumerate(d.paragraphs, 1):
    t = (p.text or '').strip()
    if t:
        ra.append('[P%03d %s] %s' % (i, p.style.name, t))
ra.append('\n=== BẢNG: %d ===' % len(d.tables))
for ti, tb in enumerate(d.tables, 1):
    ra.append('--- Bảng %d: %d hàng x %d cột' % (ti, len(tb.rows), len(tb.columns)))
    for ri, row in enumerate(tb.rows, 1):
        o = [(c.text or '').replace('\n', ' / ').strip() for c in row.cells]
        ra.append('  r%02d | %s' % (ri, ' | '.join(o))[:600])
txt = '\n'.join(ra)
io.open(P, 'w', encoding='utf-8').write(txt)
print('  đã trích %d đoạn + %d bảng -> %d ký tự' % (len(d.paragraphs), len(d.tables), len(txt)))
print('  file: %s' % P)
