Hướng dẫn python convert pdf to text ocr - python chuyển đổi pdf sang văn bản ocr

Python được sử dụng rộng rãi để phân tích dữ liệu nhưng dữ liệu không cần phải ở định dạng cần thiết luôn luôn. Trong những trường hợp như vậy, chúng tôi chuyển đổi định dạng đó (như PDF hoặc JPG, v.v.) sang định dạng văn bản, để phân tích dữ liệu theo cách tốt hơn. Python cung cấp nhiều thư viện để thực hiện nhiệm vụ này. Có một số cách để làm điều này, bao gồm cả việc sử dụng các thư viện như PYPDF2 trong Python. Nhược điểm chính của việc sử dụng các thư viện này là sơ đồ mã hóa. Các tài liệu PDF có thể có nhiều mã hóa bao gồm UTF-8, ASCII, Unicode, v.v. Vì vậy, việc chuyển đổi PDF thành văn bản có thể dẫn đến việc mất dữ liệu do sơ đồ mã hóa. Hãy cùng xem cách đọc tất cả các nội dung của tệp PDF và lưu trữ nó trong tài liệu văn bản bằng OCR. Đầu tiên, chúng ta cần chuyển đổi các trang của PDF thành hình ảnh và sau đó, sử dụng OCR (nhận dạng ký tự quang học) để đọc nội dung từ hình ảnh và lưu trữ trong tệp văn bản. & NBSP;

Cài đặt bắt buộc:

pip3 install PIL
pip3 install pytesseract
pip3 install pdf2image
sudo apt-get install tesseract-ocr

Có hai phần của chương trình như sau:

Phần số 1 liên quan đến việc chuyển đổi PDF thành các tệp hình ảnh. Mỗi trang của PDF được lưu trữ dưới dạng tệp hình ảnh. Tên của các hình ảnh được lưu trữ là: PDF Trang 1 -> Trang_1.JPG PDF Trang 2 -> Trang_2.jpg PDF Trang 3 -> Trang_3.jpg. Trang PDF N -> page_n.jpg.deals with converting the PDF into image files. Each page of the PDF is stored as an image file. The names of the images stored are: PDF page 1 -> page_1.jpg PDF page 2 -> page_2.jpg PDF page 3 -> page_3.jpg …. PDF page n -> page_n.jpg.

Phần #2 liên quan đến việc nhận dạng văn bản từ các tệp hình ảnh và lưu trữ nó vào một tệp văn bản. Ở đây, chúng tôi xử lý các hình ảnh và chuyển đổi nó thành văn bản. Khi chúng ta có văn bản dưới dạng biến chuỗi, chúng ta có thể thực hiện bất kỳ xử lý nào trên văn bản. Ví dụ, trong nhiều tệp PDF, khi một dòng được hoàn thành, nhưng một từ cụ thể không thể được viết hoàn toàn trong cùng một dòng, một dấu gạch nối (‘-) được thêm vào và từ được tiếp tục trên dòng tiếp theo. Ví dụ -deals with recognizing text from the image files and storing it into a text file. Here, we process the images and convert it into text. Once we have the text as a string variable, we can do any processing on the text. For example, in many PDFs, when a line is completed, but a particular word cannot be written entirely in the same line, a hyphen (‘-‘) is added, and the word is continued on the next line. For example –

This is some sample text but this parti-
cular word could not be written in the same line.

Bây giờ đối với những từ như vậy, một xử lý trước cơ bản được thực hiện để chuyển đổi dấu gạch nối và dòng mới thành một từ đầy đủ. Sau khi tất cả việc xử lý trước được thực hiện, văn bản này được lưu trữ trong một tệp văn bản riêng biệt. Để nhận các tệp PDF đầu vào được sử dụng trong mã, nhấp vào D.PDF. & NBSP;

Dưới đây là việc thực hiện: & NBSP;

Python3

import platform

from tempfile import TemporaryDirectory

from pathlib import

This is some sample text but this parti-
cular word could not be written in the same line.
1

import

This is some sample text but this parti-
cular word could not be written in the same line.
3

from

This is some sample text but this parti-
cular word could not be written in the same line.
5import
This is some sample text but this parti-
cular word could not be written in the same line.
7

from

This is some sample text but this parti-
cular word could not be written in the same line.
9import import1

import2 import3import4import4 import6import7

import8import9import4 platform1

platform2platform3platform4

import8platform6

import8platform8import4 from0from1platform6

import8from4import4 from0from7from8

from9import7

import8from4import4 tempfile 4tempfile 5tempfile 6

tempfile 7import4 from0import0platform6

import2import4 import4

import5import4 from4import8 tempfile 4TemporaryDirectory0platform6

TemporaryDirectory2 TemporaryDirectory3

import8TemporaryDirectory5

platform2import2 import3import4import4 import6import7

from3from4import4 from6

from7from8from9pathlib 0import4pathlib 2

from3platform6

platform2from9import7

from3from4import4

This is some sample text but this parti-
cular word could not be written in the same line.
01from9platform6

Các

from3

This is some sample text but this parti-
cular word could not be written in the same line.
14import4
This is some sample text but this parti-
cular word could not be written in the same line.
16
This is some sample text but this parti-
cular word could not be written in the same line.
17

from3

This is some sample text but this parti-
cular word could not be written in the same line.
19
This is some sample text but this parti-
cular word could not be written in the same line.
20platform6

from3

This is some sample text but this parti-
cular word could not be written in the same line.
23

platform2

This is some sample text but this parti-
cular word could not be written in the same line.
25
This is some sample text but this parti-
cular word could not be written in the same line.
26
This is some sample text but this parti-
cular word could not be written in the same line.
27
This is some sample text but this parti-
cular word could not be written in the same line.
28
This is some sample text but this parti-
cular word could not be written in the same line.
29

from3

This is some sample text but this parti-
cular word could not be written in the same line.
05
This is some sample text but this parti-
cular word could not be written in the same line.
32
This is some sample text but this parti-
cular word could not be written in the same line.
07
This is some sample text but this parti-
cular word could not be written in the same line.
34

from7

This is some sample text but this parti-
cular word could not be written in the same line.
36import4
This is some sample text but this parti-
cular word could not be written in the same line.
38
This is some sample text but this parti-
cular word could not be written in the same line.
39
This is some sample text but this parti-
cular word could not be written in the same line.
26
This is some sample text but this parti-
cular word could not be written in the same line.
41

from7

This is some sample text but this parti-
cular word could not be written in the same line.
36import4
This is some sample text but this parti-
cular word could not be written in the same line.
45
This is some sample text but this parti-
cular word could not be written in the same line.
46
This is some sample text but this parti-
cular word could not be written in the same line.
47

from7

This is some sample text but this parti-
cular word could not be written in the same line.
49

import2

This is some sample text but this parti-
cular word could not be written in the same line.
51import4import4
This is some sample text but this parti-
cular word could not be written in the same line.
54import7

import8

This is some sample text but this parti-
cular word could not be written in the same line.
57

Đầu ra: Tệp PDF đầu vào: Tệp văn bản đầu ra: Như chúng ta thấy, các trang của PDF đã được chuyển đổi thành hình ảnh. Sau đó, các hình ảnh đã được đọc và nội dung được viết vào một tệp văn bản. Ưu điểm của phương pháp này bao gồm: Input PDF file:

Hướng dẫn python convert pdf to text ocr - python chuyển đổi pdf sang văn bản ocr
Output Text file:
Hướng dẫn python convert pdf to text ocr - python chuyển đổi pdf sang văn bản ocr
As we see, the pages of the PDF were converted to images. Then the images were read, and the content was written into a text file. Advantages of this method include:

  1. Tránh chuyển đổi dựa trên văn bản vì sơ đồ mã hóa dẫn đến mất dữ liệu.
  2. Ngay cả nội dung viết tay trong PDF cũng có thể được nhận ra do sử dụng OCR.
  3. Chỉ nhận ra các trang cụ thể của PDF cũng có thể.
  4. Nhận văn bản như một biến để bất kỳ số lượng tiền xử lý được yêu cầu nào cũng có thể được thực hiện.

Nhược điểm của phương pháp này bao gồm:

  1. Lưu trữ đĩa được sử dụng để lưu trữ các hình ảnh trong hệ thống cục bộ. Mặc dù những hình ảnh này có kích thước nhỏ.
  2. Sử dụng OCR không thể đảm bảo độ chính xác 100%. Cho một tài liệu PDF được gõ bằng máy tính dẫn đến độ chính xác rất cao.
  3. Các tệp PDF viết tay vẫn được công nhận, nhưng độ chính xác phụ thuộc vào các yếu tố khác nhau như chữ viết tay, màu trang, v.v.

Làm cách nào để chuyển đổi tệp PDF thành TXT trong Python?

Cách chuyển đổi PDF thành TXT..
Cài đặt 'Aspose. Từ cho Python qua. MẠNG LƯỚI'..
Thêm một tham chiếu thư viện (nhập thư viện) vào dự án Python của bạn ..
Mở tệp PDF nguồn trong Python ..
Gọi phương thức 'Lưu ()', chuyển tên tệp đầu ra với phần mở rộng TXT ..
Nhận kết quả chuyển đổi PDF là TXT ..

Làm cách nào để chuyển đổi PDF thành OCR?

Mở tệp PDF chứa hình ảnh được quét trong Acrobat cho Mac hoặc PC.Nhấp vào công cụ Chỉnh sửa PDF PDF ở khung bên phải.Acrobat tự động áp dụng nhận dạng ký tự quang (OCR) cho tài liệu của bạn và chuyển đổi nó thành một bản sao hoàn toàn có thể chỉnh sửa PDF của bạn.Nhấp vào phần tử văn bản bạn muốn chỉnh sửa và bắt đầu gõ.

Làm cách nào để tạo PDF có thể tìm kiếm trong Python?

Chuyển đổi PDF được quét thành PDF có thể tìm kiếm trong Python..
Thêm tệp thư mục ..
Yêu cầu cài đặt mô -đun ..
Khóa API của bạn ..
Tệp nguồn và tên tệp PDF ..
Chạy chương trình Python ..
PDF làm cho văn bản có thể tìm kiếm được demo API ..

Python có thể đọc văn bản từ pdf không?

Gói Pypdf của Python có thể được sử dụng để đạt được những gì chúng ta muốn (trích xuất văn bản), mặc dù nó có thể làm nhiều hơn những gì chúng ta cần.Gói này cũng có thể được sử dụng để tạo, giải mã và hợp nhất các tệp PDF., although it can do more than what we need. This package can also be used to generate, decrypting and merging PDF files.