我有:

我有一个PDF,这是在两列的format.Is,有没有办法阅读每个PDF的两列格式,而不裁剪每个PDF单独?
发布于 2021-09-24 14:20:18
我找到了另一种方法,你可以把pdf裁剪成两部分,左边和右边,然后合并每个页面的左边和右边的内容,你可以试试这个:
# https://github.com/jsvine/pdfplumber
import pdfplumber
x0 = 0 # Distance of left side of character from left side of page.
x1 = 0.5 # Distance of right side of character from left side of page.
y0 = 0 # Distance of bottom of character from bottom of page.
y1 = 1 # Distance of top of character from bottom of page.
all_content = []
with pdfplumber.open("file_path") as pdf:
for i, page in enumerate(pdf.pages):
width = page.width
height = page.height
# Crop pages
left_bbox = (x0*float(width), y0*float(height), x1*float(width), y1*float(height))
page_crop = page.crop(bbox=left_bbox)
left_text = page_crop.extract_text()
left_bbox = (0.5*float(width), y0*float(height), 1*float(width), y1*float(height))
page_crop = page.crop(bbox=left_bbox)
right_text = page_crop.extract_text()
page_context = '\n'.join([left_text, right_text])
all_content.append(page_context)
if i < 2: # help you see the merged first two pages
print(page_context)发布于 2019-03-12 00:00:07
这是我用于常规pdf解析的代码,它在图像上似乎工作得很好(我下载了一个图像,所以这使用光学字符识别,所以它与常规OCR一样准确)。请注意,这是文本的标记化。还要注意的是,您需要安装tesseract才能正常工作(pytesseract只是让tesseract在python中工作)。Tesseract是免费和开源的。
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)https://stackoverflow.com/questions/55100037
复制相似问题