Как извлечь из PDF-файла только часть таблицы с помощью pdfplumber?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как извлечь из PDF-файла только часть таблицы с помощью pdfplumber?

Сообщение Anonymous »

Я пытаюсь использовать pdfplumber для извлечения ТОЛЬКО определенных данных из таблицы PDF-файла в CSV-файл. Это изображение стола, на который я смотрю.
Изображение

На данный момент я нахожусь на этапе записи таблицы в файл Excel. Вот код, который у меня есть:

Код: Выделить всё

import pdfplumber
import csv
import os
# Define extraction regions for each table (adjust coordinates as needed)
regions = [
(10, 100, 600, 260),
]
# Region for Table 1
# Add more regions for additional tables if needed

# Define the desired headers

# Specify the directory and filename for saving the CSV file
output_directory = "C:/Users/myname/Downloads"
output_filename = "clients_info.csv"
output_path = os.path.join(output_directory, output_filename)

with pdfplumber.open("C:/Users/myname/Downloads/clients.pdf") as pdf:
for region_index, region in enumerate(regions):
x1, y1, x2, y2 = region
tables_data = []  # Store data for all tables in this region

page = pdf.pages[0]  # Extracting tables from the first page
table = page.within_bbox((x1, y1, x2, y2)).extract_table()

# Extract header row and filter out None values
header_row = [cell for cell in table[0] if cell is not None]

# Extract data rows and remove None values
for row in table[1:]:
filtered_row = [cell if cell is not None else "" for cell in row]
tables_data.append(filtered_row)

# Write the data for this region to a CSV file
with open(output_path, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header_row)  # Write the filtered header row to the CSV file
for row in tables_data:
writer.writerow(row)  # Write the data rows to the CSV file
Однако я хочу написать только заголовки, выделенные красным цветом в первой строке Excel, и соответствующие данные (белые ячейки красного цвета) во второй строке. В конце концов, я хотел бы просмотреть весь 200-страничный PDF-файл, поскольку каждая таблица представляет 1 человека, а всего их 100 человек. Как мне улучшить его, чтобы печатать только те, которые выделены красным?
Большое спасибо за помощь.

Подробнее здесь: https://stackoverflow.com/questions/784 ... pdfplumber
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»