如何使用 Python 从两列 pdf 中提取文本? [英] How to extract text from two column pdf with Python?

查看:77
本文介绍了如何使用 Python 从两列 pdf 中提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

我有一个两栏格式的 PDF.有没有办法根据两栏格式阅读每个 PDF,而无需单独裁剪每个 PDF?

I have a PDF which are in two-column format.Is there a way to read each PDF according to the two-column format without cropping each PDF individually?

推荐答案

这是我用于常规 pdf 解析的代码,它似乎在该图像上运行正常(我下载了一个图像,所以它使用了光学字符识别,所以它与常规 OCR 一样准确).请注意,这会标记文本.另请注意,您需要安装 tesseract 才能使其工作(pytesseract 只是让 tesseract 从 python 中工作).Tesseract 是免费的开源.

This is code I use for regular pdf parsing, and it seems to work ok on that image (I downloaded an image, so this uses Optical Character Recognition, so its as accurate as regular OCR). Note that this tokenizes the text. Also note that you need to install tesseract for this to work (pytesseract just makes tesseract work from python). Tesseract is free & open source.

from PIL import Image
import pytesseract
import cv2
import os

def parse(image_path, threshold=False, blur=False):
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if threshold:
        gray = cv2.threshold(gray, 0, 255, \
            cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    if blur: #useful if salt-and-pepper background.
        gray = cv2.medianBlur(gray, 3)
    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray) #Create a temp file
    text = pytesseract.image_to_string(Image.open(filename))
    os.remove(filename) #Remove the temp file
    text = text.split() #PROCESS HERE.
    print(text)
a = parse(image_path, True, False)

这篇关于如何使用 Python 从两列 pdf 中提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆