# -*- coding: utf-8 -*-
r"""B90: bóc bảng PDF (find_tables) 3 kỳ + in lưới để đối chiếu."""
import os, glob
import pymupdf

DIR = r'C:\Claw 2\Nghien cuu\Tinh-Gia-Thanh\GIA-NGUYEN-LIEU\_mail_hang_1310'
for f in ['GIÁ DUNG MÔI 13.10.26.pdf'] + [os.path.join('ky_truoc', x) for x in ('GIÁ DUNG MÔI 06.10.26.pdf', 'GIÁ DUNG MÔI 29.09.26.pdf')]:
    p = os.path.join(DIR, f)
    d = pymupdf.open(p)
    print('=' * 100)
    print('FILE:', f, '| trang:', d.page_count)
    for pg in d:
        ts = pg.find_tables()
        print('  số bảng:', len(ts.tables))
        for i, t in enumerate(ts.tables):
            g = t.extract()
            print('  --- bảng %d: %d hàng x %d cột' % (i, len(g), max(len(r) for r in g)))
            for j, row in enumerate(g):
                cells = [(c or '').replace('\n', ' | ').strip() for c in row]
                print('   %3d  %s' % (j, ' ~ '.join(cells)))
