import pdfplumber import re import json files = [ '/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/DaoTao/2.-CHUONG-TRINH-CAN-THIEP-MACH-MAU-THAN-KINH-NANG-CAO-BO-SUNG-FINAL.pdf', '/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/DaoTao/CHUONG-TRINH-CAN-THIEP-MACH-MAU-CAC-TANG-VA-MACH-MAU-NGOAI-BIEN-CO-BAN.pdf', '/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/DaoTao/CHUONG-TRINH-CHUAN-BI-DUNG-CU-VA-CHAM-SOC-BENH-NHAN-TRONG-PHONG-CHUP-MACH.pdf', '/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/DaoTao/CHUONG-TRINH-DAO-TAO-TIM-MACH-CAN-THIEP-1.pdf' ] slugs = [ "can-thiep-mach-mau-than-kinh-nang-cao", "can-thiep-mach-mau-cac-tang-va-ngoai-bien-co-ban", "chuan-bi-dung-cu-va-cham-soc-benh-nhan-trong-phong-chup-mach", "tim-mach-can-thiep-co-ban" ] def extract_section(text, start_keywords, end_keywords=None): lines = text.split('\n') started = False content = [] for line in lines: line_lower = line.lower() if not started: for k in start_keywords: if k in line_lower: started = True # Optional: remove the keyword from the line to get just the value # But simpler to just keep it or try to split split_idx = line_lower.find(k) + len(k) val = line[split_idx:].strip(': ').strip() if val: content.append(val) break else: if end_keywords: for k in end_keywords: if k in line_lower: return '\n'.join(content).strip() # Stop if we hit a new numbered section like "10." or empty lines after some text if re.match(r'^\d+\.', line) or re.match(r'^[A-Z\s]+$', line): return '\n'.join(content).strip() if line.strip(): content.append(line.strip()) return '\n'.join(content).strip() for f, slug in zip(files, slugs): print(f"--- Parsing {slug} ---") with pdfplumber.open(f) as pdf: text = '' for page in pdf.pages: text += page.extract_text() + '\n' time = extract_section(text, ['thời gian đào tạo']) audience = extract_section(text, ['đối tượng học viên', 'đối tượng tham dự', 'đối tượng']) quantity = extract_section(text, ['số lượng học viên:', 'số lượng học viên']) documents = extract_section(text, ['hồ sơ đăng ký tham dự', 'hồ sơ nhập học']) tuitionFee = extract_section(text, ['học phí']) location = extract_section(text, ['địa điểm nhận hồ sơ']) print(f"Thời gian: {time[:50]}") print(f"Đối tượng: {audience[:50]}") print(f"Số lượng: {quantity[:50]}") print(f"Hồ sơ: {documents[:50]}") print(f"Học phí: {tuitionFee[:50]}") print(f"Địa điểm: {location[:50]}") print("")