Hướng dẫn how do i extract data from a pdf table in python? - làm cách nào để trích xuất dữ liệu từ bảng pdf trong python?

Trích xuất dữ liệu từ các bảng trong PDF - Sử dụng một dòng duy nhất trong Python

Trong bài viết này, bạn sẽ tìm hiểu cách tốt nhất để trích xuất và xuất dữ liệu từ các bảng trong các tệp PDF sang CSV/Excel bằng Python.

Ảnh của David Clode trên unplash

Tìm nạp các bảng từ các tệp PDF không còn là một nhiệm vụ khó khăn nữa, bạn có thể thực hiện việc này bằng một dòng duy nhất trong Python.

Những gì bạn sẽ học

  1. Cài đặt một thư viện Tabula-Py.
  2. Nhập thư viện.
  3. Đọc một tệp PDF.
  4. Đọc một bảng trên một trang cụ thể của tệp pdf.
  5. Đọc nhiều bảng trên cùng một trang của tệp PDF.
  6. Chuyển đổi tệp PDF trực tiếp sang tệp CSV.

Tabula

Tabula là một trong những gói hữu ích không chỉ cho phép bạn xóa các bảng từ các tệp PDF mà còn chuyển đổi tệp PDF trực tiếp thành tệp CSV.

Vậy hãy bắt đầu…

1. Cài đặt thư viện Tabula-Py

pip install tabula-py

2. Nhập thư viện Tabula

import tabula

3. Đọc tệp PDF

Cho phép loại bỏ bản PDF này vào khung dữ liệu PANDAS.PDF into pandas Data Frame.

Hình ảnh của Satya Ganesh
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]

Hãy xem đầu ra của đoạn mã trên được thực hiện trong Google Colabs

Hình ảnh của Satya Ganesh

4. Đọc một bảng trên một trang cụ thể của tệp PDF.

Giả sử chúng ta cần loại bỏ tệp PDF này chứa nhiều trang trong đó.PDF FILE which contains multiple pages in it.

Hình ảnh của Satya Ganeshimage bởi Satya Ganesh
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])

5. Điều gì xảy ra nếu có nhiều bảng trên cùng một trang của tệp PDF?

Giả sử chúng ta cần cạo 2 bảng này trên cùng một trang của tệp PDF. PDF file.

Hình ảnh của Satya Ganesh
To read multiple tables we need to add extra parametermultiple_tables = True -> Read multiple tables as independent tables
multiple_tables = False -> Read multiple tables as single table

5.1. Đọc nhiều bảng là bảng độc lập

file3 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data3.pdf"tables = tabula.read_pdf(file3 ,pages=1, multiple_tables=True)print(tables[0])
print(tables[1])
Image bởi Satya Ganeshimage của Satya Ganesh

5.2 Đọc nhiều bảng dưới dạng một bảng

tables = tabula.read_pdf(file3 ,pages=1,multiple_tables=False)tables[0]
Image bởi Satya Ganesh

6. Covert một tệp PDF trực tiếp vào tệp CSV

Chúng tôi có thể trực tiếp chuyển đổi tệp PDF chứa dữ liệu bảng trực tiếp sang tệp CSV bằng phương thức Convert_into () trong Thư viện Tabula.convert_into() method in tabula library.

1. Chuyển đổi bảng trong 1 trang của tệp PDF thành CSV

# output just the first page tables in the PDF to a CSVtabula.convert_into("pdf_file_name", "Name_of_csv_file.csv")

2. Chuyển đổi tất cả bảng trong tệp PDF thành CSV

tabula.convert_into("pdf_file_name","Name_of_csv_file.csv",all = True)

Sự kết luận

Tôi hy vọng bạn đã học được một cách tuyệt vời để cạo các bảng tệp PDF bằng một dòng duy nhất trong Python.

Kiểm tra các bài viết liên quan của tôi

Tài liệu tham khảo

Cảm ơn vì đã đọc 😃 Chúc một ngày tốt lành

Chủ đề này là về cách trích xuất các bảng từ PDF Enter Python. Lúc đầu, hãy để thảo luận về những gì mà một tệp PDF?

PDF (định dạng tài liệu di động) có thể là một định dạng tệp đã nắm bắt được tất cả thời tiết của một tài liệu in dưới dạng bitmap mà bạn chỉ có thể xem, điều hướng, in hoặc chuyển tiếp cho người khác. Các tệp PDF được tạo bằng Adobe Acrobat,

Thí dụ :

Giả sử một tệp pdf chứa một bảng

Tên người dùngTênNghề nghiệp
1 DavidQuản lý sản phẩm
2 Sư TửQuản trị viên IT
3 JohnLuật sư

Và chúng tôi muốn đọc bảng này vào chương trình Python của chúng tôi. Vấn đề này có thể được giải quyết bằng cách sử dụng một số phương pháp. Hãy để thảo luận từng người một.

Phương pháp 1: Sử dụng Tabula-Py

Tabula-Py là một trình bao bọc Python đơn giản của Tabula-Java, có thể đọc các bảng trong PDF. Bạn có thể cài đặt thư viện Tabula-Py bằng lệnh.

pip install tabula-py
pip install tabulate

Các phương pháp được sử dụng trong ví dụ là:

read_pdf (): Đọc dữ liệu từ các bảng của tệp PDF của địa chỉ đã cho reads the data from the tables of the PDF file of the given address

Tabulation (): Sắp xếp dữ liệu theo định dạng bảng arranges the data in a table format

Tệp PDF được sử dụng ở đây là PDF.

Python3

import tabula
1
import tabula
2
import tabula
3
import tabula
4

import tabula
1
import tabula
6
import tabula
3
import tabula
8

import tabula
9
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
1
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
2223
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
5
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
6

file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
7
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
8

Output:

Hướng dẫn how do i extract data from a pdf table in python? - làm cách nào để trích xuất dữ liệu từ bảng pdf trong python?

Phương pháp 2: Sử dụng Camelot

Camelot là một thư viện Python giúp trích xuất các bảng từ các tệp PDF. Bạn có thể cài đặt thư viện Camelot-Py bằng lệnh

import tabula
0

Các phương pháp được sử dụng trong ví dụ là:

read_pdf (): Đọc dữ liệu từ các bảng của tệp PDF của địa chỉ đã cho reads the data from the tables of the pdf file of the given address

Bảng [INDEX] .DF: Điểm hướng về bảng mong muốn của một chỉ mục nhất địnhpoints towards the desired table of a given index

Tệp PDF được sử dụng ở đây là PDF.

Hướng dẫn how do i extract data from a pdf table in python? - làm cách nào để trích xuất dữ liệu từ bảng pdf trong python?

Python3

import tabula
3
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
0

file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
1
file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
0
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
3
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
4
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
5

file1 = "https://nbviewer.jupyter.org/github/kuruvasatya/Scraping-Tables-from-PDF/blob/master/data1.pdf"table = tabula.read_pdf(file1,pages=1)table[0]
7
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
7
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
8
file2 = "https://nbviewer.jupyter.org/github/kuruvasatya/Reading-Table-Data-From-PDF/blob/master/data.pdf"# To read table in first page of PDF file
table1 = tabula.read_pdf(file2 ,pages=1)
# To read tables in secord page of PDF file
table2 = tabula.read_pdf(file2 ,pages=2)
print(table1[0])
print(table2[0])
9

Output:


Tôi có thể trích xuất dữ liệu từ PDF bằng Python không?

Thư viện Python phổ biến..
PDFMiner là một công cụ để trích xuất thông tin từ các tài liệu PDF. ....
PYPDF2 là một thư viện PDF Python thuần túy có khả năng chia tách, hợp nhất với nhau, cắt xén và chuyển đổi các trang của các tệp PDF. ....
Tabula-Py là một trình bao bọc Python đơn giản của Tabula-Java, có thể đọc bảng của PDF ..

Làm cách nào để trích xuất dữ liệu từ bảng PDF?

Đây là cách bạn có thể trích xuất các bảng từ tệp PDF bằng Excel:..
Mở bảng tính Excel của bạn ..
Chuyển đến tab Dữ liệu ..
Trong phần GET & Transform, nhấp vào GET dữ liệu ..
Từ danh sách, chọn từ tệp và sau đó chọn từ PDF.....
Chọn tệp PDF bạn muốn trích xuất các bảng từ ..
Nhấp vào mở ..

Làm cách nào để trích xuất một bảng từ hình ảnh PDF trong Python?

Tôi sẽ đề nghị bạn trích xuất bảng bằng cách sử dụng tabula ...
Sử dụng Tesseract để phát hiện vòng quay và hình ảnh Mogrify để sửa nó ..
Sử dụng OpenCV để tìm và trích xuất bảng ..
Sử dụng OpenCV để tìm và trích xuất từng ô từ bảng ..

Làm thế nào để bạn trích xuất dữ liệu từ một bảng trong Python?

Các bước để tìm nạp các hàng từ bảng cơ sở dữ liệu MySQL..
Kết nối với MySQL từ Python.....
Xác định truy vấn chọn SQL.....
Nhận đối tượng con trỏ từ kết nối.....
Thực thi truy vấn chọn bằng phương thức Execute ().....
Trích xuất tất cả các hàng từ một kết quả.....
Lặp lại mỗi hàng.....
Đóng đối tượng đối tượng con trỏ và đối tượng cơ sở dữ liệu ..