PDFDocHandler
dcmspec.pdf_doc_handler.PDFDocHandler
Bases: DocHandler
Handler class for extracting tables from PDF documents.
Provides methods to download, cache, and extract tables as CSV data from PDF files.
Source code in src/dcmspec/pdf_doc_handler.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
__init__(config=None, logger=None, extractor='pdfplumber')
Initialize the PDF document handler.
Sets up the handler with an optional configuration and logger.
PARAMETER | DESCRIPTION |
---|---|
config
|
Configuration object for cache and other settings.
TYPE:
|
logger
|
Logger instance to use. If None, a default logger is created.
TYPE:
|
extractor
|
Table extraction library to use.
TYPE:
|
Source code in src/dcmspec/pdf_doc_handler.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
concat_tables(tables, table_id=None)
Concatenate selected tables (across pages or by specification) into a single logical table.
PARAMETER | DESCRIPTION |
---|---|
tables
|
List of table dicts, each with 'page', 'index', 'header', and 'data'.
TYPE:
|
table_id
|
An identifier for the concatenated table.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dict with keys 'table_id' (if provided), 'header' (from the first table),
TYPE:
|
dict
|
and 'data' (the concatenated table as a list of rows). |
Source code in src/dcmspec/pdf_doc_handler.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|
download(url, cache_file_name, progress_observer=None, progress_callback=None)
Download and cache a PDF file from a URL using the base class download method.
PARAMETER | DESCRIPTION |
---|---|
url
|
The URL of the PDF document to download.
TYPE:
|
cache_file_name
|
The filename of the cached document.
TYPE:
|
progress_observer
|
Optional observer to report download progress.
TYPE:
|
progress_callback
|
[LEGACY, Deprecated] Optional callback to report progress as an integer percent (0-100, or -1 if indeterminate). Use progress_observer instead. Will be removed in a future release.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The file path where the document was saved. |
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the download or save fails. |
Source code in src/dcmspec/pdf_doc_handler.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
extract_notes(pdf, page_numbers, table_id=None, note_pattern='^\\d*\\s*Note\\s\\d+:', header_footer_pattern='^\\s*(IHE|_{3,}|Rev\\.|Copyright|Template|Page\\s\\d+|\\(TDW-II\\))', line_number_pattern='^\\d+\\s', end_note_pattern='.*7\\.5\\.1\\.1\\.2')
Extract notes referenced in tables from the specified PDF pages.
PARAMETER | DESCRIPTION |
---|---|
pdf
|
The PDF object.
TYPE:
|
page_numbers
|
List of page numbers (1-indexed) to extract notes from.
TYPE:
|
table_id
|
The table_id to associate with these notes.
TYPE:
|
note_pattern
|
Regex pattern to identify note lines.
TYPE:
|
header_footer_pattern
|
Regex pattern to skip header/footer lines.
TYPE:
|
line_number_pattern
|
Regex pattern to remove line numbers.
TYPE:
|
end_note_pattern
|
Regex pattern to identify the end of notes section.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
Mapping from note key (e.g., "Note 1:") to a dict with 'text' and 'table_id' (if provided).
TYPE:
|
Example return
{ "Note 1:": {"text": "...", "table_id": "T-7.5-1"}, "Note 2:": {"text": "...", "table_id": "T-7.5-1"}, }
Source code in src/dcmspec/pdf_doc_handler.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
extract_tables_camelot(file_path, page_numbers)
Extract and return all tables from the specified PDF pages using Camelot.
Uses Camelot in "lattice" mode, which detects tables by analyzing the rendered page image for drawn lines.
PARAMETER | DESCRIPTION |
---|---|
file_path
|
Path to the PDF file.
TYPE:
|
page_numbers
|
List of page numbers (1-indexed) to extract tables from.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[dict]
|
List[dict]: List of dicts, each with keys 'page', 'index', and 'data' (table as list of rows). |
Source code in src/dcmspec/pdf_doc_handler.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
extract_tables_pdfplumber(pdf, page_numbers)
Extract and return all tables from the specified PDF pages using pdfplumber.
Uses pdfplumber to extract tables from the PDF by analyzing lines and whitespace in the PDF's vector content.
PARAMETER | DESCRIPTION |
---|---|
pdf
|
The PDF object.
TYPE:
|
page_numbers
|
List of page numbers (1-indexed) to extract tables from.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[dict]
|
List[dict]: List of dicts, each with keys 'page', 'index', and 'data' (table as list of rows). |
RAISES | DESCRIPTION |
---|---|
IndexError
|
If a page number is out of range for the PDF. |
Source code in src/dcmspec/pdf_doc_handler.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
load_document(cache_file_name, url=None, force_download=False, progress_observer=None, progress_callback=None, page_numbers=None, table_indices=None, table_header_rowspan=None, table_id=None)
Download, cache, and extract the logical CSV table from the PDF.
PARAMETER | DESCRIPTION |
---|---|
cache_file_name
|
Path to the local cached PDF file.
TYPE:
|
url
|
URL to download the file from if not cached or if force_download is True.
TYPE:
|
force_download
|
If True, do not use cache and download the file from the URL.
TYPE:
|
progress_observer
|
Optional observer to report download progress.
TYPE:
|
progress_callback
|
[LEGACY, Deprecated] Optional callback to report progress as an integer percent (0-100, or -1 if indeterminate). Use progress_observer instead. Will be removed in a future release.
TYPE:
|
page_numbers
|
List of page numbers to extract tables from.
TYPE:
|
table_indices
|
List of (page, index) tuples specifying which tables to concatenate.
TYPE:
|
table_header_rowspan
|
Number of header rows (rowspan) for each table in table_indices.
TYPE:
|
table_id
|
An identifier for the concatenated table.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
The specification table dict with keys 'header', 'data', and optionally 'table_id'.
TYPE:
|
Example
handler = PDFDocHandler()
spec_table = handler.load_document(
cache_file_name="myfile.pdf",
url="https://example.com/myfile.pdf",
page_numbers=[10, 11],
table_indices=[(10, 0), (11, 1)],
table_header_rowspan={
(10, 0): 2, # Table starts on page 10, index 0 and has 2 header rows
(11, 1): 2, # Table ends on page 11, index 1 and has 2 header rows
},
table_id="my_spec_table"
)
Source code in src/dcmspec/pdf_doc_handler.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
select_tables(tables, table_indices, table_header_rowspan=None)
Select tables referenced by table_indices and split each table into header and data.
This method processes a list of extracted tables, selects those specified by table_indices, and splits each selected table into a header and data rows. If a table has multiple header rows (as specified by table_header_rowspan), these rows are merged column-wise to form a single header row.
PARAMETER | DESCRIPTION |
---|---|
tables
|
List of table dicts, each with 'page', 'index', and 'data' (raw table rows).
TYPE:
|
table_indices
|
List of (page, index) tuples specifying which tables to select and process.
TYPE:
|
table_header_rowspan
|
Number of header rows (rowspan) for each table in table_indices, keyed by (page, index). If not specified, defaults to 1 header row per table.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[dict]
|
List[dict]: List of dicts, each with keys: - 'page': page number of the table - 'index': index of the table on the page - 'header': merged header row (list of column names) - 'data': list of data rows (list of cell values) |
Example
selected_tables = handler.select_tables(
tables,
table_indices=[(10, 0), (11, 1)],
table_header_rowspan={(10, 0): 2, (11, 1): 2}
)
for table in selected_tables:
print(table["header"], table["data"])
Source code in src/dcmspec/pdf_doc_handler.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|