Skip to main content

将电子书自动转换为交互式知识图谱 HTML

Project description

kg-book-tool

将电子书(PDF / EPUB / DOCX / PPTX / XLSX / HTML / Markdown / 图片)自动转换为交互式知识图谱

PyPI version Python 3.8+ License: MIT

✨ 特性

  • 📚 多格式支持 — PDF / EPUB / DOCX / PPTX / XLSX / HTML / Markdown / TXT
  • 🖼️ OCR 回退 — 自动检测图像型 PDF 并调用 EasyOCR(中文支持)
  • 🔤 编码自动检测 — UTF-8 / GBK / GB2312 / GB18030 / Big5 / Latin-1
  • 📖 参考文献识别 — 自动将"参考文献"小节的编号条目(如 [1] Singhal)提取为实体
  • 🌐 桌面应用 — Tkinter 原生 GUI(双击启动)
  • 🎯 三视图阅读器 — 实体列表、思维导图、原文对照

📦 安装

pip install kg-book-tool

默认已包含 PDF / EPUB / DOCX / PPTX / XLSX / HTML / OCR 全套依赖,无需手动安装额外组件。

🚀 快速开始

命令行

# 从电子书中构建知识图谱
kg-build book.pdf --output data.json --js data.js

# 启用 LLM 摘要
kg-build book.epub --summary --model qwen2.5:7b

# 支持的格式:pdf / epub / docx / pptx / xlsx / html / md / txt / 图片
kg-build book.docx --output data.json

Python API

from kg_core import KnowledgeGraphBuilder

builder = KnowledgeGraphBuilder("book.pdf", model_name="qwen2.5:3b")
builder.load_book()                                # 自动识别格式
builder.extract_entities()                         # 章节标题 + 参考文献
builder.extract_definitions_and_paragraphs()        # 实体定义与上下文
builder.build_relations()                          # 实体关系
data = builder.build()                             # 返回完整字典

builder.save_json("data.json")
builder.save_js("data.js")                         # 前端可用
builder.save_md("data.md", book_name="我的书")      # 原文按章节保存

桌面应用

# 命令行启动
kg-desktop
# 或
python -m kg_app.desktop

Tkinter 原生窗口,包含:

  • 选择电子文件
  • 一键构建
  • 实时查看知识图谱
  • 实体浏览 / 关系网络 / 原文对照

🛠️ 支持的输入格式

格式 扩展名 解析器
PDF(文本型) .pdf PyMuPDF
PDF(图像型) .pdf EasyOCR(自动回退)
EPUB .epub ebooklib
Word .docx python-docx
PowerPoint .pptx python-pptx
Excel .xlsx openpyxl
HTML .html / .htm beautifulsoup4
Markdown .md 内置
纯文本 .txt 内置(自动检测编码)
图片 .png / .jpg / ... EasyOCR

📊 输出示例

$ kg-build book.pdf --output data.json --js data.js
[kg-build] 开始构建知识图谱...
  书籍: book.pdf
  模型: qwen2.5:3b
  摘要:   JSON: data.json
  JS:   data.js

构建完成!
  实体: 304
  关系: 26832
  有摘要: 0
  有定义: 220
  有段落: 295

📂 项目结构

kg-book-tool/
├── kg_core/                  # 核心库(pip 入口)
│   ├── __init__.py
│   ├── builder.py            # KnowledgeGraphBuilder:构建流程
│   ├── converters.py         # 多格式转换 + OCR 回退
│   ├── text_cleaner.py       # 编码检测与文本清洗
│   ├── summarizer.py         # LLM 摘要(Ollama)
│   └── cli.py                # kg-build 命令行
├── kg_app/                   # Web + 桌面应用
│   ├── desktop.py            # Tkinter 桌面入口
│   ├── main.py               # Flask 后端
│   ├── gui.py                # 旧版 GUI
│   ├── templates/            # 渲染模板
│   │   ├── index.html
│   │   └── reader.html
│   └── static/
│       ├── data/             # 生成的知识图谱数据
│       └── uploads/          # 用户上传的电子文件
├── scripts/                  # 辅助脚本
├── setup.py                  # pip 打包配置
├── requirements.txt
├── prompt.json               # LLM prompt 配置
├── deploy.ps1                # 一键部署脚本
└── DEPLOY.md                 # 部署指南

🧪 验证安装

import kg_core
print(kg_core.__version__)               # '1.0.0'

from kg_core import (
    KnowledgeGraphBuilder,
    SummaryGenerator,
    clean_text,
    split_chapters,
)
print("All imports OK")

🐛 故障排查

问题 解决
ModuleNotFoundError: No module named 'fitz' pip install pymupdf
easyocr 首次运行慢 首次会下载模型(约 100MB)
中文乱码 项目已内置 chardet + charset_normalizer 自动检测
图像型 PDF 提取为空 项目已内置 EasyOCR 回退
参考文献未识别 实体识别优先用后端 is_reference 字段,详见 kg_core/builder.py

📜 许可

MIT License — 详见 LICENSE

🔗 链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kg_book_tool-1.0.1.tar.gz (129.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kg_book_tool-1.0.1-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file kg_book_tool-1.0.1.tar.gz.

File metadata

  • Download URL: kg_book_tool-1.0.1.tar.gz
  • Upload date:
  • Size: 129.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kg_book_tool-1.0.1.tar.gz
Algorithm Hash digest
SHA256 da6f986b187f5e4ad2d4f19e5e395606885b3aa2b8927f804a019fcf752b6c64
MD5 4e2a00217f385b6cae03ffc1fac88969
BLAKE2b-256 f423b977feecf24c91dc77dcd797d7e8bcefa2e220b364851606284537231d7d

See more details on using hashes here.

File details

Details for the file kg_book_tool-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: kg_book_tool-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kg_book_tool-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f55a1b043c86bc2849ac054be992867255041d83e38bf668fa3b675f49720082
MD5 06a852aff3fb6db7b4771c1be0a2b5b9
BLAKE2b-256 1bad722c0d5d699a42bf8dd9b5efb9e62d1248bf5724e7abb7abf1c21c6e4bae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page