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; Show 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
This is some sample text but this parti- cular word could not be written in the same line.1
This is some sample text but this parti- cular word could not be written in the same line.3
This is some sample text but this parti- cular word could not be written in the same line.5 import This is some sample text but this parti- cular word could not be written in the same line.7
This is some sample text but this parti- cular word could not be written in the same line.9 import import 1
This is some sample text but this parti- cular word could not be written in the same line.01 from 9platform 6Các
This is some sample text but this parti- cular word could not be written in the same line.14 import 4 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
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.20 platform 6
This is some sample text but this parti- cular word could not be written in the same line.23
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
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
This is some sample text but this parti- cular word could not be written in the same line.36 import 4 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
This is some sample text but this parti- cular word could not be written in the same line.36 import 4 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
This is some sample text but this parti- cular word could not be written in the same line.49
This is some sample text but this parti- cular word could not be written in the same line.51 import 4import 4 This is some sample text but this parti- cular word could not be written in the same line.54 import 7
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: Output Text file: 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:
Nhược điểm của phương pháp này bao gồm:
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. |