AI-video-lifeplan-demo/backend/analyze_materials.py

152 lines
5.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Analyze PPT and PDF materials to extract main content structure
"""
import os
import sys
from pathlib import Path
from pptx import Presentation
import fitz # PyMuPDF
class MaterialAnalyzer:
def __init__(self):
self.project_root = Path(__file__).parent.parent
def analyze_ppt(self):
"""Analyze PPT structure and content"""
print("=" * 80)
print("📊 PPT分析结果")
print("=" * 80)
ppt_path = self.project_root / "assets/0_input/ライフプラン情報.pptx"
prs = Presentation(str(ppt_path))
print(f"\n总页数: {len(prs.slides)}页\n")
for idx, slide in enumerate(prs.slides):
print(f"\n【第{idx + 1}页】")
print("-" * 60)
# Extract title
if slide.shapes.title:
print(f"标题: {slide.shapes.title.text}")
# Extract text content
text_content = []
for shape in slide.shapes:
if hasattr(shape, "text") and shape.text.strip():
if shape != slide.shapes.title:
text_content.append(shape.text.strip())
if text_content:
print(f"\n内容摘要:")
for i, text in enumerate(text_content[:5], 1): # Show first 5 items
preview = text[:100] + "..." if len(text) > 100 else text
print(f" {i}. {preview}")
if len(text_content) > 5:
print(f" ... (还有{len(text_content) - 5}项内容)")
print(f"\n文本框数量: {len(text_content)}")
def analyze_pdf(self):
"""Analyze PDF structure and content"""
print("\n\n" + "=" * 80)
print("📄 PDF分析结果")
print("=" * 80)
pdf_path = self.project_root / "assets/0_input/帳票(お客様向け).pdf"
doc = fitz.open(str(pdf_path))
print(f"\n总页数: {len(doc)}页\n")
for page_num in range(min(10, len(doc))): # Analyze first 10 pages
page = doc[page_num]
text = page.get_text()
print(f"\n【第{page_num + 1}页】")
print("-" * 60)
# Extract key information
lines = [line.strip() for line in text.split('\n') if line.strip()]
if lines:
print(f"页面内容预览:")
for i, line in enumerate(lines[:15], 1): # Show first 15 lines
preview = line[:80] + "..." if len(line) > 80 else line
print(f" {i}. {preview}")
if len(lines) > 15:
print(f" ... (还有{len(lines) - 15}行内容)")
else:
print(" (此页可能为图像或表格)")
print(f"\n文本行数: {len(lines)}")
if len(doc) > 10:
print(f"\n... (还有{len(doc) - 10}页未显示)")
doc.close()
def find_family_info_pages(self):
"""Find pages containing family information"""
print("\n\n" + "=" * 80)
print("🔍 家庭信息页面定位")
print("=" * 80)
pdf_path = self.project_root / "assets/0_input/帳票(お客様向け).pdf"
doc = fitz.open(str(pdf_path))
keywords = ["テスト太郎", "家族", "年齢", "年収", "生年月日", "基本情報", "家族構成"]
print(f"\n搜索关键词: {', '.join(keywords)}\n")
matches = []
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
found_keywords = [kw for kw in keywords if kw in text]
if found_keywords:
matches.append((page_num, found_keywords, text))
if matches:
print(f"找到{len(matches)}个包含家庭信息的页面:\n")
for page_num, keywords_found, text in matches[:5]: # Show first 5
print(f"【PDF第{page_num + 1}页】(pdfPageIndex={page_num})")
print(f" 匹配关键词: {', '.join(keywords_found)}")
# Show excerpt
lines = [line.strip() for line in text.split('\n') if line.strip()]
print(f" 内容片段:")
for line in lines[:8]:
preview = line[:70] + "..." if len(line) > 70 else line
print(f" {preview}")
print()
else:
print("未找到包含家庭信息的页面")
doc.close()
def run(self):
"""Execute full analysis"""
try:
self.analyze_ppt()
self.analyze_pdf()
self.find_family_info_pages()
print("\n" + "=" * 80)
print("✅ 分析完成")
print("=" * 80)
except Exception as e:
print(f"\n❌ 分析失败: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
analyzer = MaterialAnalyzer()
analyzer.run()