ToB企服应用市场:ToB评测及商务社交产业平台

标题: 别再为文本提取抓狂!一站式文本提取神器Kreuzberg 助你解决PDF、图片、文 [打印本页]

作者: 农民    时间: 2025-2-21 06:39
标题: 别再为文本提取抓狂!一站式文本提取神器Kreuzberg 助你解决PDF、图片、文
大家好,我是六哥,相信许多朋友肯定都有过从各种文档里提取文本的经历,那过程可太让人头疼了!本日就给大家分享一款超实用的当代Python库——Kreuzberg,帮你轻松解决文本提取的难题。
一、Kreuzberg解决了什么问题

现在许多文本提取工具,要么依靠外部API调用,要么配置特别复杂,使用起来很不方便。而Kreuzberg专为解决RAG(检索增强生成)应用里的文本提取需求而生,不外它可不止这点用处,任何文本提取场景都能美满适配。它专注于本地处理,依靠少,简朴又高效。
二、Kreuzberg的强大功能

三、Kreuzberg的显著特点

四、使用方法超简朴

  1. from pathlib import Path
  2. from kreuzberg import extract_file, extract_bytes
  3. # 基本文件提取
  4. async def extract_document():
  5.     # 从PDF文件提取
  6.     pdf_result = await extract_file("document.pdf")
  7.     print(f"PDF文本: {pdf_result.content}")
  8.     # 从图像提取
  9.     img_result = await extract_file("scan.png")
  10.     print(f"图像文本: {img_result.content}")
  11.     # 从Word文档提取
  12.     docx_result = await extract_file(Path("document.docx"))
  13.     print(f"Word文本: {docx_result.content}")
复制代码
  1. - `extract_bytes()`:从字节中提取文本,接受字节字符串。比如处理上传的文件:
复制代码
  1. from kreuzberg import extract_bytes
  2. async def process_upload(file_content: bytes, mime_type: str):
  3.     """处理已知MIME类型的上传文件内容。"""
  4.     result = await extract_bytes(file_content, mime_type=mime_type)
  5.     return result.content
  6. # 不同文件类型的示例用法
  7. async def handle_uploads():
  8.     # 处理PDF上传
  9.     pdf_result = await extract_bytes(pdf_bytes, mime_type="application/pdf")
  10.     # 处理图像上传
  11.     img_result = await extract_bytes(image_bytes, mime_type="image/jpeg")
  12.     # 处理Word文档上传
  13.     docx_result = await extract_bytes(docx_bytes,
  14.         mime_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
复制代码
  1. from kreuzberg import extract_file
  2. async def process_pdf():
  3.     # 对包含图像或扫描内容的PDF强制OCR
  4.     result = await extract_file("document.pdf", force_ocr=True)
  5.     # 处理扫描版PDF(自动使用OCR)
  6.     scanned = await extract_file("scanned.pdf")
复制代码
  1. - **提取结果对象**:所有提取函数返回的对象包含提取的文本(`content`)和输出格式(`mime_type`)。
复制代码
  1. from kreuzberg import ExtractionResult
  2. async def process_document(path: str) -> tuple[str, str]:
  3.     # 作为具名元组访问
  4.     result: ExtractionResult = await extract_file(path)
  5.     print(f"内容: {result.content}")
  6.     print(f"格式: {result.mime_type}")
  7.     # 或解包为元组
  8.     content, mime_type = await extract_file(path)
  9.     return content, mime_type
复制代码
  1. - **错误处理**:Kreuzberg通过多种异常类型提供全面的错误处理,所有异常都继承自`KreuzbergError` ,每个异常都包含有助于调试的上下文信息。
复制代码
  1. from kreuzberg import extract_file
  2. from kreuzberg.exceptions import (
  3.     ValidationError,
  4.     ParsingError,
  5.     OCRError,
  6.     MissingDependencyError
  7. )
  8. async def safe_extract(path: str) -> str:
  9.     try:
  10.         result = await extract_file(path)
  11.         return result.content
  12.     except ValidationError as e:
  13.         # 输入验证问题
  14.         # - 不支持或无法检测的MIME类型
  15.         # - 文件缺失
  16.         # - 无效输入参数
  17.         print(f"验证失败: {e}")
  18.     except OCRError as e:
  19.         # OCR特定问题
  20.         # - Tesseract处理失败
  21.         # - 图像转换问题
  22.         print(f"OCR失败: {e}")
  23.     except MissingDependencyError as e:
  24.         # 系统依赖问题
  25.         # - 缺少Tesseract OCR
  26.         # - 缺少Pandoc
  27.         # - 版本不兼容
  28.         print(f"依赖缺失: {e}")
  29.     except ParsingError as e:
  30.         # 一般处理错误
  31.         # - PDF解析失败
  32.         # - 格式转换问题
  33.         # - 编码问题
  34.         print(f"处理失败: {e}")
  35.     return ""
  36. # 示例错误上下文
  37. try:
  38.     result = await extract_file("document.xyz")
  39. except ValidationError as e:
  40.     # 错误将包含上下文:
  41.     # ValidationError: 不支持的mime类型
  42.     # 上下文: {
  43.     #    "file_path": "document.xyz",
  44.     #    "supported_mimetypes": ["application/pdf",...]
  45.     # }
  46.     print(e)
  47. try:
  48.     result = await extract_file("scan.jpg")
  49. except OCRError as e:
  50.     # 错误将包含上下文:
  51.     # OCRError: OCR返回非0代码失败
  52.     # 上下文: {
  53.     #    "file_path": "scan.jpg",
  54.     #    "tesseract_version": "5.3.0"
  55.     # }
  56.     print(e)
复制代码
五、支持格式超丰富

六、架构设计很巧妙

Kreuzberg设计为在现有开源工具之上的高级异步抽象,集成了多个工具来实现强大功能:
如果你对Kreuzberg感爱好,想深入相识或者参与开发,可以访问项目链接:https://github.com/Goldziher/kreuzberg

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4