溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本

發(fā)布時(shí)間:2021-10-11 21:22:26 來(lái)源:億速云 閱讀:246 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本”,在日常操作中,相信很多人在如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

目錄
  • 一、前言

    • 1.1、為什么不使用傳統(tǒng)的pdf 轉(zhuǎn)文本工具呢?

  • 二、實(shí)現(xiàn)過(guò)程

    • 2.1、基于深度學(xué)習(xí)的 OCR 將 pdf 為文本

      • 2.1.1、將 pdf 轉(zhuǎn)換為圖像

      • 2.1.2、檢測(cè)和識(shí)別圖像中的文本

      • 2.1.3、示例輸出

一、前言

對(duì)很多人來(lái)說(shuō),將PDF轉(zhuǎn)換為可編輯的文本是個(gè)剛需,卻苦于沒(méi)有簡(jiǎn)單的方法。發(fā)現(xiàn) pdf 幻燈片,效果還不錯(cuò)。

傳統(tǒng)的講座通常伴隨有很多pdf幻燈片。一般來(lái)說(shuō),想要對(duì)自己的講座做筆記,需要從pdf復(fù)制、補(bǔ)充大量?jī)?nèi)容。

最近,來(lái)自 K1 Digital 的高級(jí)機(jī)器工程師 Lucas Soares 一直在嘗試通過(guò)使用 CR(光學(xué)字符識(shí)別)自動(dòng) pdf 幻燈片,以便直接在 Markdown 文件中操作它們的內(nèi)容,從而避免手動(dòng)復(fù)制和粘貼 pdf 內(nèi)容,實(shí)現(xiàn)這個(gè)過(guò)程的自動(dòng)化。

如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本

圖為項(xiàng)目作者盧卡斯·蘇亞雷斯。

1.1、為什么不使用傳統(tǒng)的pdf 轉(zhuǎn)文本工具呢?

Lucas Soares 發(fā)現(xiàn)傳統(tǒng)工具往往會(huì)帶來(lái)更多的問(wèn)題,需要花時(shí)間解決。他曾嘗試使用傳統(tǒng)的 Python 軟件包,但遇到了很多問(wèn)題(例如必須使用復(fù)雜的正則表達(dá)式模式解析最終輸出等),因此決定嘗試使用目標(biāo)檢測(cè)和 OCR 來(lái)解決。

二、實(shí)現(xiàn)過(guò)程

基本過(guò)程可分為以下幾個(gè)步驟:

  • 將 pdf 轉(zhuǎn)換為圖片;

  • 檢測(cè)和識(shí)別圖像中的文本;

  • 展示示例輸出。

2.1、基于深度學(xué)習(xí)的 OCR 將 pdf 為文本

2.1.1、將 pdf 轉(zhuǎn)換為圖像

Soares 使用的 pdf 幻燈片來(lái)自于 David Silver 的增長(zhǎng)學(xué)習(xí)(參見(jiàn)以下 pdf 幻燈片地址)。使用「pdf2image」包將每張幻燈片轉(zhuǎn)換為 png 圖像格式。

如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本

pdf 幻燈片示例。

地址:https://www.davidsilver.uk/wp-content/uploads/2020/03/intro_RL.pdf

代碼如下:

from pdf2image import convert_from_path
from pdf2image.exceptions import (
 PDFInfoNotInstalledError,
 PDFPageCountError,
 PDFSyntaxError
)
pdf_path = "path/to/file/intro_RL_Lecture1.pdf"
images = convert_from_path(pdf_path)
for i, image in enumerate(images):
    fname = "image" + str(i) + ".png"
    image.save(fname, "PNG")

經(jīng)過(guò)處理后,所有的pdf幻燈片都轉(zhuǎn)換成png格式的圖片:

如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本

2.1.2、檢測(cè)和識(shí)別圖像中的文本

為了檢測(cè)和識(shí)別png圖像中的文本,Soares使用ocr.pytorch庫(kù)中的文本檢測(cè)器。按照說(shuō)明下載模型保存模型保存在檢查點(diǎn)文件夾中。

ocr.pytorch 庫(kù)地址:https://github.com/courao/ocr.pytorch

代碼如下:

# adapted from this source: https://github.com/courao/ocr.pytorch
%load_ext autoreload
%autoreload 2
import os
from ocr import ocr
import time
import shutil
import numpy as np
import pathlib
from PIL import Image
from glob import glob
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import pytesseract
def single_pic_proc(image_file):
    image = np.array(Image.open(image_file).convert('RGB'))
    result, image_framed = ocr(image)
    return result,image_framed
image_files = glob('./input_images/*.*')
result_dir = './output_images_with_boxes/'
# If the output folder exists we will remove it and redo it.
if os.path.exists(result_dir):
    shutil.rmtree(result_dir)
os.mkdir(result_dir)
for image_file in sorted(image_files):
    result, image_framed = single_pic_proc(image_file) # detecting and recognizing the text
    filename = pathlib.Path(image_file).name
    output_file = os.path.join(result_dir, image_file.split('/')[-1])
    txt_file = os.path.join(result_dir, image_file.split('/')[-1].split('.')[0]+'.txt')
    txt_f = open(txt_file, 'w')
    Image.fromarray(image_framed).save(output_file)
    for key in result:
        txt_f.write(result[key][1]+'\n')
    txt_f.close()

設(shè)置輸入和輸出文件夾,接著遍歷所有輸入圖像(轉(zhuǎn)換后的pdf幻燈片),然后通過(guò)single_pic_proc()函數(shù)運(yùn)行OCR模塊中的檢測(cè)和識(shí)別模型,最后將輸出保存到輸出文件夾。

從檢測(cè)繼承(inherit)了Pytorch CTPN,識(shí)別了Pytorch CRNN,模型都存在于OCR模塊中。

2.1.3、示例輸出

代碼如下:

import cv2 as cv
output_dir = pathlib.Path("./output_images_with_boxes")
# image = cv.imread(str(np.random.choice(list(output_dir.iterdir()),1)[0]))
image = cv.imread(f"{output_dir}/image7.png")
size_reshaped = (int(image.shape[1]),int(image.shape[0]))
image = cv.resize(image, size_reshaped)
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()

下圖左為原始pdf 幻燈片,圖右為腦后的輸出文本,準(zhǔn)確率非常高。

如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本

文本識(shí)別輸出如下:

filename = f"{output_dir}/image7.txt"
with open(filename, "r") as text:
    for line in text.readlines():
        print(line.strip("\n"))

通過(guò)上述方法,最終可以得到一個(gè)非常強(qiáng)大的工具來(lái)討論文檔,從檢測(cè)和識(shí)別手寫(xiě)筆記到檢測(cè)和識(shí)別照片中的隨機(jī)。

擁有文本的 OCR 工具來(lái)處理一些文本內(nèi)容,這比依賴外部軟件來(lái)說(shuō)明文檔要好得多。

到此,關(guān)于“如何用Python實(shí)現(xiàn)PDF轉(zhuǎn)換文本”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI