经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
利用Python提取PDF文本的简单方法实例
来源:jb51  时间:2022/7/25 15:26:38  对本文有异议

你好,一般情况下,Ctrl+C 是最简单的方法,当无法 Ctrl+C 时,我们借助于 Python,以下是具体步骤:

第一步,安装工具库

1、tika — 用于从各种文件格式中进行文档类型检测和内容提取

2、wand — 基于 ctypes 的简单 ImageMagick 绑定

3、pytesseract — OCR 识别工具

创建一个虚拟环境,安装这些工具

  1. python -m venv venv
  2. source venv/bin/activate
  3. pip install tika wand pytesseract

第二步,编写代码

假如 pdf 文件里面既有文字,又有图片,以下代码可以直接识别文字:

  1. import io
  2. import pytesseract
  3. import sys
  4. from PIL import Image
  5. from tika import parser
  6. from wand.image import Image as wi
  7. text_raw = parser.from_file("example.pdf")
  8. print(text_raw['content'].strip())

这还不够,我们还需要能失败图片的部分:

  1. def extract_text_image(from_file, lang='deu', image_type='jpeg', resolution=300):
  2. print("-- Parsing image", from_file, "--")
  3. print("---------------------------------")
  4. pdf_file = wi(filename=from_file, resolution=resolution)
  5. image = pdf_file.convert(image_type)
  6. image_blobs = []
  7. for img in image.sequence:
  8. img_page = wi(image=img)
  9. image_blobs.append(img_page.make_blob(image_type))
  10. extract = []
  11. for img_blob in image_blobs:
  12. image = Image.open(io.BytesIO(img_blob))
  13. text = pytesseract.image_to_string(image, lang=lang)
  14. extract.append(text)
  15. for item in extract:
  16. for line in item.split("\n"):
  17. print(line)

合并一下,完整代码如下:

  1. import io
  2. import sys
  3. from PIL import Image
  4. import pytesseract
  5. from wand.image import Image as wi
  6. from tika import parser
  7. def extract_text_image(from_file, lang='deu', image_type='jpeg', resolution=300):
  8. print("-- Parsing image", from_file, "--")
  9. print("---------------------------------")
  10. pdf_file = wi(filename=from_file, resolution=resolution)
  11. image = pdf_file.convert(image_type)
  12. for img in image.sequence:
  13. img_page = wi(image=img)
  14. image = Image.open(io.BytesIO(img_page.make_blob(image_type)))
  15. text = pytesseract.image_to_string(image, lang=lang)
  16. for part in text.split("\n"):
  17. print("{}".format(part))
  18. def parse_text(from_file):
  19. print("-- Parsing text", from_file, "--")
  20. text_raw = parser.from_file(from_file)
  21. print("---------------------------------")
  22. print(text_raw['content'].strip())
  23. print("---------------------------------")
  24. if __name__ == '__main__':
  25. parse_text(sys.argv[1])
  26. extract_text_image(sys.argv[1], sys.argv[2])

第三步,执行

假如 example.pdf 是这样的:

1f7cd9c778e1c3cfe5b9a0d36ea69272.png

在命令行这样执行:

  1. python run.py example.pdf deu | xargs -0 echo > extract.txt

最终 extract.txt 的结果如下:

-- Parsing text example.pdf --
---------------------------------
Title pure text
 
Content pure text
 
    Slide 1
    Slide 2
---------------------------------
-- Parsing image example.pdf --
---------------------------------
Title pure text
 
Content pure text
 
Title in image
 
Text in image

你可能会问,如果是简体中文,那个 lang 参数传递什么,传 'chi_sim',其实是有官方说明的,链接如下:

https://github.com/tesseract-ocr/tessdoc/blob/main/Data-Files-in-different-versions.md

0361bb52f8ded0538c545369c2c0254f.png

最后的话

从 PDF 中提取文本的脚本实现并不复杂,许多库简化了工作并取得了很好的效果

到此这篇关于利用Python提取PDF文本的简单方法的文章就介绍到这了,更多相关Python提取PDF文本内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号