Working in the PDF mine
pdfminer.six is widely used for text extraction and layout analysis
due to its liberal licensing terms. Unfortunately it is quite slow
and contains many bugs. Now you can use PLAYA instead:
from playa.miner import extract, LAParams
laparams = LAParams()
for page in extract(path, laparams):
# do something
This is generally faster than pdfminer.six. You can often make it
even faster on large documents by running in parallel with the
max_workers argument, which is the same as the one you will find in
concurrent.futures.ProcessPoolExecutor. If you pass None it will
use all your CPUs, but due to some unavoidable overhead, it usually
doesn't help to use more than 2-4:
for page in extract(path, laparams, max_workers=2):
# do something
There are a few differences with pdfminer.six (some might call them
bug fixes):
- By default, if you do not pass the
laparamsargument toextract, no layout analysis at all is done. This is different fromextract_pagesinpdfminer.sixwhich will set some default parameters for you. If you don't see anyLTTextBoxitems in yourLTPagethen this is why! - Rectangles are recognized correctly in some cases where
pdfminer.sixthought they were "curves". - Colours and colour spaces are the PLAYA versions, which do not
correspond to what
pdfminer.sixgives you, because whatpdfminer.sixgives you is not useful and often wrong. - You have access to the list of enclosing marked content sections in
every
LTComponent, as themcstackattribute. - Bounding boxes of rotated glyphs are the actual bounding box.
Probably more... but you didn't use any of that stuff anyway, you just
wanted to get LTTextBoxes to feed to your hallucination factories.
Reference
playa.miner
Reimplementation of pdfminer.six layout analysis on top of PLAYA.
GraphicState
dataclass
PDF graphics state (PDF 1.7 section 8.4) including text state (PDF 1.7 section 9.3.1), but excluding coordinate transformations.
Contrary to the pretensions of pdfminer.six, the text state is for
the most part not at all separate from the graphics state, and can
be updated outside the confines of BT and ET operators, thus
there is no advantage and only confusion that comes from treating
it separately.
The only state that does not persist outside BT / ET pairs is
the text coordinate space (line matrix and text rendering matrix),
and it is also the only part that is updated during iteration over
a TextObject.
For historical reasons the main coordinate transformation matrix, though it is also part of the graphics state, is also stored separately.
Attributes:
| Name | Type | Description |
|---|---|---|
clipping_path |
None
|
The current clipping path (sec. 8.5.4) |
linewidth |
float
|
Line width in user space units (sec. 8.4.3.2) |
linecap |
int
|
Line cap style (sec. 8.4.3.3) |
linejoin |
int
|
Line join style (sec. 8.4.3.4) |
miterlimit |
float
|
Maximum length of mitered line joins (sec. 8.4.3.5) |
dash |
DashPattern
|
Dash pattern for stroking (sec 8.4.3.6) |
intent |
PSLiteral
|
Rendering intent (sec. 8.6.5.8) |
stroke_adjustment |
bool
|
A flag specifying whether to compensate for possible rasterization effects when stroking a path with a line width that is small relative to the pixel resolution of the output device (sec. 10.7.5) |
blend_mode |
Union[PSLiteral, List[PSLiteral]]
|
The current blend mode that shall be used in the transparent imaging model (sec. 11.3.5) |
smask |
Union[None, Dict[str, PDFObject]]
|
A soft-mask dictionary (sec. 11.6.5.1) or None |
salpha |
float
|
The constant shape or constant opacity value used for stroking operations (sec. 11.3.7.2 & 11.6.4.4) |
nalpha |
float
|
The constant shape or constant opacity value used for non-stroking operations |
alpha_source |
bool
|
A flag specifying whether the current soft mask and alpha constant parameters shall be interpreted as shape values (true) or opacity values (false). This flag also governs the interpretation of the SMask entry, if any, in an image dictionary |
black_pt_comp |
PSLiteral
|
The black point compensation algorithm that shall be used when converting CIE-based colours (sec. 8.6.5.9) |
flatness |
float
|
The precision with which curves shall be rendered on the output device (sec. 10.6.2) |
scolor |
Color
|
Colour used for stroking operations |
scs |
ColorSpace
|
Colour space used for stroking operations |
ncolor |
Color
|
Colour used for non-stroking operations |
ncs |
ColorSpace
|
Colour space used for non-stroking operations |
font |
Union[Font, None]
|
The current font. |
fontsize |
float
|
The "font size" parameter, which is not the font size in points as you might understand it, but rather a scaling factor applied to text space (so, it affects not only text size but position as well). Since most reasonable people find that behaviour rather confusing, this is often just 1.0, and PDFs rely on the text matrix to set the size of text. |
charspace |
float
|
Extra spacing to add after each glyph, expressed in
unscaled text space units, meaning it is not affected by
|
wordspace |
float
|
Extra spacing to add after a space glyph, defined
very specifically as the glyph encoded by the single-byte
character code 32 (SPOILER: it is probably a space). Also
expressed in unscaled text space units, but modified by
|
scaling |
float
|
The horizontal scaling factor as defined by the PDF standard (that is, divided by 100). |
leading |
float
|
The leading as defined by the PDF standard, in unscaled text space units. |
render_mode |
int
|
The PDF rendering mode. The really important one here is 3, which means "don't render the text". You might want to use this to detect invisible text. |
rise |
float
|
The text rise (superscript or subscript position), in unscaled text space units. |
knockout |
bool
|
The text knockout flag, shall determine the behaviour of overlapping glyphs within a text object in the transparent imaging model (sec. 9.3.8) |
Source code in playa/content.py
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 | |
LAParams
Parameters for layout analysis
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_overlap
|
float
|
If two characters have more overlap than this they are considered to be on the same line. The overlap is specified relative to the minimum height of both characters. |
0.5
|
char_margin
|
float
|
If two characters are closer together than this margin they are considered part of the same line. The margin is specified relative to the width of the character. |
2.0
|
word_margin
|
float
|
If two characters on the same line are further apart than this margin then they are considered to be two separate words, and an intermediate space will be added for readability. The margin is specified relative to the width of the character. |
0.1
|
line_margin
|
float
|
If two lines are are close together they are considered to be part of the same paragraph. The margin is specified relative to the height of a line. |
0.5
|
boxes_flow
|
Optional[float]
|
Specifies how much a horizontal and vertical position of a text matters when determining the order of text boxes. The value should be within the range of -1.0 (only horizontal position matters) to +1.0 (only vertical position matters). You can also pass |
0.5
|
detect_vertical
|
bool
|
If vertical text should be considered during layout analysis |
False
|
all_texts
|
bool
|
If layout analysis should be performed on text in figures. |
False
|
Source code in playa/miner.py
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 | |
LTAnno
Actual letter in the text as a Unicode string.
Note that, while a LTChar object has actual boundaries, LTAnno objects does not, as these are "virtual" characters, inserted by a layout analyzer according to the relationship between two characters (e.g. a space).
Source code in playa/miner.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
LTChar
Bases: LTComponent, LTText
Actual letter in the text as a Unicode string.
Source code in playa/miner.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
LTComponent
Bases: LTItem
Object with a bounding box
Source code in playa/miner.py
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 | |
LTContainer
Bases: LTComponent, Generic[LTItemT]
Object that can be extended and analyzed
Source code in playa/miner.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
LTCurve
Bases: LTComponent
A generic Bezier curve
The parameter original_path contains the original
pathing information from the pdf (e.g. for reconstructing Bezier Curves).
dashing_style contains the Dashing information if any.
Source code in playa/miner.py
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 | |
LTFigure
Bases: LTLayoutContainer
Represents an area used by PDF Form objects.
PDF Forms can be used to present figures or pictures by embedding yet another PDF document within a page. Note that LTFigure objects can appear recursively.
Source code in playa/miner.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | |
LTImage
Bases: LTComponent
An image object.
Embedded images can be in JPEG, Bitmap or JBIG2.
Source code in playa/miner.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
LTItem
Interface for things that can be analyzed
Source code in playa/miner.py
326 327 328 329 330 331 | |
analyze(laparams)
Perform the layout analysis.
Source code in playa/miner.py
330 331 | |
LTLayoutContainer
Bases: LTContainer[LTComponent]
Source code in playa/miner.py
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 | |
group_textboxes(laparams, boxes)
Group textboxes hierarchically.
Get pair-wise distances, via dist func defined below, and then merge from the closest textbox pair. Once obj1 and obj2 are merged / grouped, the resulting group is considered as a new object, and its distances to other objects & groups are added to the process queue.
For performance reason, pair-wise distances and object pair info are maintained in a heap of (idx, dist, id(obj1), id(obj2), obj1, obj2) tuples. It ensures quick access to the smallest element. Note that since comparison operators, e.g., lt, are disabled for LTComponent, id(obj) has to appear before obj in element tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
laparams
|
LAParams
|
LAParams object. |
required |
boxes
|
Sequence[LTTextBox]
|
All textbox objects to be grouped. |
required |
Returns:
| Type | Description |
|---|---|
List[LTTextGroup]
|
a list that has only one element, the final top level group. |
Source code in playa/miner.py
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 | |
group_textlines(laparams, lines)
Group neighboring lines to textboxes
Source code in playa/miner.py
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
LTLine
Bases: LTCurve
A single straight line.
Could be used for separating text or figures.
Source code in playa/miner.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
LTPage
Bases: LTLayoutContainer
Represents an entire page.
Like any other LTLayoutContainer, an LTPage can be iterated to obtain child objects like LTTextBox, LTFigure, LTImage, LTRect, LTCurve and LTLine.
Source code in playa/miner.py
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | |
LTRect
Bases: LTCurve
A rectangle.
Could be used for framing another pictures or figures.
Source code in playa/miner.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
LTText
Interface for things that have text
Source code in playa/miner.py
334 335 336 337 338 339 340 341 342 343 | |
get_text()
Text contained in this object
Source code in playa/miner.py
341 342 343 | |
LTTextBox
Bases: LTTextContainer[LTTextLine]
Represents a group of text chunks in a rectangular area.
Note that this box is created by geometric analysis and does not necessarily represents a logical boundary of the text. It contains a list of LTTextLine objects.
Source code in playa/miner.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 | |
LTTextLine
Bases: LTTextContainer[TextLineElement]
Contains a list of LTChar objects that represent a single text line.
The characters are aligned either horizontally or vertically, depending on the text's writing mode.
Source code in playa/miner.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
LTTextLineHorizontal
Bases: LTTextLine
Source code in playa/miner.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 | |
find_neighbors(plane, ratio)
Finds neighboring LTTextLineHorizontals in the plane.
Returns a list of other LTTestLineHorizontals in the plane which are close to self. "Close" can be controlled by ratio. The returned objects will be the same height as self, and also either left-, right-, or centrally-aligned.
Source code in playa/miner.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
LTTextLineVertical
Bases: LTTextLine
Source code in playa/miner.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | |
find_neighbors(plane, ratio)
Finds neighboring LTTextLineVerticals in the plane.
Returns a list of other LTTextLineVerticals in the plane which are close to self. "Close" can be controlled by ratio. The returned objects will be the same width as self, and also either upper-, lower-, or centrally-aligned.
Source code in playa/miner.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
NameTree
A PDF name tree.
See Section 7.9.6 of the PDF 1.7 Reference.
Source code in playa/data_structures.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
NumberTree
A PDF number tree.
See Section 7.9.7 of the PDF 1.7 Reference.
Source code in playa/data_structures.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
PDFDocument
Representation of a PDF document.
Since PDF documents can be very large and complex, merely creating
a Document does very little aside from verifying that the
password is correct and getting a minimal amount of metadata. In
general, PLAYA will try to open just about anything as a PDF, so
you should not expect the constructor to fail here if you give it
nonsense (something else may fail later on).
Some metadata, such as the structure tree and page tree, will be loaded lazily and cached. We do not handle modification of PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
Union[BinaryIO, bytes]
|
File-like object in binary mode, or a buffer with binary data.
Files will be read using |
required |
password
|
str
|
Password for decryption, if needed. |
''
|
space
|
DeviceSpace
|
the device space to use for interpreting content ("screen" or "page") |
'screen'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
if |
PDFEncryptionError
|
if the PDF has an unsupported encryption scheme |
PDFPasswordIncorrect
|
if the password is incorrect |
Source code in playa/document.py
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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | |
destinations
property
Named destinations as an iterable/addressable Destinations object.
fonts
property
Get the mapping of font names to fonts for this document.
Note that this can be quite slow the first time it's accessed as it must scan every single page in the document.
Note: Font names may collide.
Font names are generally understood to be globally unique
in the neighbourhood in the document, but there's no
guarantee that this is the case. In keeping with the
"incremental update" philosophy dear to PDF, you get the
last font with a given name.
Danger: Do not rely on this being a dict.
Currently this is implemented eagerly, but in the future it
may return a lazy object which only loads fonts on demand.
names
property
PDF name dictionary (PDF 1.7 sec 7.7.4).
Raises:
| Type | Description |
|---|---|
KeyError
|
if nonexistent. |
objects
property
Iterate over all indirect objects (including, then expanding object streams)
outline
property
Document outline, if any.
page_labels
property
Generate page label strings for the PDF document.
If the document includes page labels, generates strings, one per page. If not, raise KeyError.
The resulting iterator is unbounded (because the page label
tree does not actually include all the pages), so it is
recommended to use pages instead.
Raises:
| Type | Description |
|---|---|
KeyError
|
No page labels are present in the catalog |
pages
property
Pages of the document as an iterable/addressable PageList object.
structure
property
Logical structure of this document, if any.
In the case where no logical structure tree exists, this will
be None. Otherwise you may iterate over it, search it, etc.
We do this instead of simply returning an empty structure tree
because the vast majority of PDFs have no logical structure.
Also, because the structure is a lazy object (the type
signature here may change to Iterable[Element] at some
point) there is no way to know if it's empty without iterating
over it.
tokens
property
Iterate over tokens.
__getitem__(objid)
Get an indirect object from the PDF.
Note that the behaviour in the case of a non-existent object
(raising IndexError), while Pythonic, is not PDFic, as PDF
1.7 sec 7.3.10 states:
An indirect reference to an undefined object shall not be considered an error by a conforming reader; it shall be treated as a reference to the null object.
Raises:
| Type | Description |
|---|---|
ValueError
|
if Document is not initialized |
IndexError
|
if objid does not exist in PDF |
Source code in playa/document.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
__iter__()
Iterate over top-level IndirectObject (does not expand object streams)
Source code in playa/document.py
358 359 360 361 362 363 364 365 | |
PDFObjRef
Source code in playa/pdftypes.py
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 | |
__init__(doc, objid)
Reference to a PDF object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Union[DocumentRef, None]
|
The PDF document. |
required |
objid
|
int
|
The object number. |
required |
Source code in playa/pdftypes.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
PDFPage
An object that holds the information about a page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
a Document object. |
required |
pageid
|
int
|
the integer PDF object ID associated with the page in the page tree. |
required |
attrs
|
Dict
|
a dictionary of page attributes. |
required |
label
|
Optional[str]
|
page label string. |
required |
page_idx
|
int
|
0-based index of the page in the document. |
0
|
space
|
DeviceSpace
|
the device space to use for interpreting content |
'screen'
|
Attributes:
| Name | Type | Description |
|---|---|---|
pageid |
the integer object ID associated with the page in the page tree |
|
attrs |
a dictionary of page attributes. |
|
resources |
Dict[str, PDFObject]
|
a dictionary of resources used by the page. |
mediabox |
the physical size of the page. |
|
cropbox |
the crop rectangle of the page. |
|
rotate |
the page rotation (in degree). |
|
label |
the page's label (typically, the logical page number). |
|
page_idx |
0-based index of the page in the document. |
|
ctm |
coordinate transformation matrix from default user space to page's device space |
Source code in playa/page.py
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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
annotations
property
Lazily iterate over page annotations.
contents
property
Iterator over PDF objects in the content streams.
doc
property
Get associated document if it exists.
fonts
property
Mapping of resource names to fonts for this page.
Note: This is not the same as playa.Document.fonts.
The resource names (e.g. F1, F42, FooBar) here are
specific to a page (or Form XObject) resource dictionary
and have no relation to the font name as commonly
understood (e.g. Helvetica,
WQERQE+Arial-SuperBold-HJRE-UTF-8). Since font names are
generally considered to be globally unique, it may be
possible to access fonts by them in the future.
Note: This does not include fonts specific to Form XObjects.
Since it is possible for the resource names to collide,
this will only return the fonts for a page and not for any
Form XObjects invoked on it. You may use
XObjectObject.fonts to access these.
Danger: Do not rely on this being a dict.
Currently this is implemented eagerly, but in the future it
may return a lazy object which only loads fonts on demand.
glyphs
property
Iterator over lazy glyph objects.
height
property
Width of the page in default user space units.
images
property
Iterator over lazy image objects.
marked_content
property
Mapping of marked content IDs to iterators over content objects.
These are the content objects associated with the structural
elements in Page.structure. So, for instance, you can do:
for element, contents in zip(page.structure,
page.marked_content):
if element is not None:
if contents is not None:
for obj in contents:
... # do something with it
Or you can also access the contents of a single element:
if page.marked_content[mcid] is not None:
for obj in page.marked_content[mcid]:
... # do something with it
Why do you have to check if it's None? Because the values
are not necessarily sequences (they may just be positions in
the content stream), it isn't possible to know if they are
empty without iterating over them, which you may or may not
want to do, because you are Lazy.
mcid_texts
property
Mapping of marked content IDs to Unicode text strings.
For use in text extraction from tagged PDFs. This is a
special case of marked_content which only cares about
extracting text (and thus is quite a bit more efficient).
Danger: Do not rely on this being a dict.
Currently this is implemented eagerly, but in the future it
may return a lazy object.
parent_key
property
Parent tree key for this page, if any.
paths
property
Iterator over lazy path objects.
streams
property
Return resolved content streams.
structure
property
Mapping of marked content IDs to logical structure elements.
This is a sequence of logical structure elements, or None
for unused marked content IDs. Note that because structure
elements may contain multiple marked content sections, the
same element may occur multiple times in this list.
It also has find and find_all methods which allow you to
access enclosing structural elements (you can also use the
parent method of elements for that)
Note: This is not the same as playa.Document.structure.
PDF documents have logical structure, but PDF pages do
not, and it is dishonest to pretend otherwise (as some
code I once wrote unfortunately does). What they do have
is marked content sections which correspond to content
items in the logical structure tree.
texts
property
Iterator over lazy text objects.
tokens
property
Iterator over tokens in the content streams.
width
property
Width of the page in default user space units.
xobjects
property
Return resolved and rendered Form XObjects.
This does not return any image or PostScript XObjects. You
can get images via the images property. Apparently you
aren't supposed to use PostScript XObjects for anything, ever.
Note that these are the XObjects as rendered on the page, so
you may see the same named XObject multiple times. If you
need to access their actual definitions you'll have to look at
page.resources.
This will also return Form XObjects within Form XObjects, except in the case of circular reference chains.
__iter__()
Iterator over lazy layout objects.
Source code in playa/page.py
262 263 264 | |
extract_text()
Do some best-effort text extraction.
This necessarily involves a few heuristics, so don't get your hopes up. It will attempt to use marked content information for a tagged PDF, otherwise it will fall back on the character displacement and line matrix to determine word and line breaks.
Source code in playa/page.py
493 494 495 496 497 498 499 500 501 502 503 504 | |
extract_text_tagged()
Get text from a page of a tagged PDF.
Source code in playa/page.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
extract_text_untagged()
Get text from a page of an untagged PDF.
Source code in playa/page.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
flatten(filter_class=None)
flatten() -> Iterator[ContentObject]
flatten(filter_class: Type[CO]) -> Iterator[CO]
Iterate over content objects, recursing into form XObjects.
Source code in playa/page.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
set_initial_ctm(space, rotate)
Set or update initial coordinate transform matrix.
PDF 1.7 section 8.4.1: Initial value: a matrix that transforms default user coordinates to device coordinates.
We keep this as self.ctm in order to transform layout
attributes in tagged PDFs which are specified in default
user space (PDF 1.7 section 14.8.5.4.3, table 344)
If you wish to modify the rotation or the device space of the
page, then you can do it with this method (the initial values
are in the rotate and space properties).
Source code in playa/page.py
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 | |
PDFTypeError
Bases: PDFException
TypeError, but for PDFs (not a subclass of TypeError, unlike in pdfminer.six)
Source code in playa/miner.py
109 110 111 112 113 114 | |
PDFValueError
Bases: PDFException
ValueError, but for PDFs (not a subclass of ValueError, unlike in pdfminer.six)
Source code in playa/miner.py
117 118 119 120 121 122 | |
PSLiteral
A class that represents a PostScript literal.
Postscript literals are used as identifiers, such as variable names, property names and dictionary keys. Literals are case sensitive and denoted by a preceding slash sign (e.g. "/Name")
Note: Do not create an instance of PSLiteral directly. Always use PSLiteralTable.intern().
Source code in playa/pdftypes.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
Plane
Bases: Generic[LTComponentT]
A set-like data structure for objects placed on a plane.
Can efficiently find objects in a certain rectangular area. It maintains two parallel lists of objects, each of which is sorted by its x or y coordinate.
Source code in playa/miner.py
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 | |
add(obj)
Place an object.
Source code in playa/miner.py
210 211 212 213 214 215 216 217 218 219 220 | |
find(bbox)
Finds objects that are in a certain area.
Source code in playa/miner.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
remove(obj)
Displace an object.
Source code in playa/miner.py
222 223 224 225 226 227 228 229 | |
decode_text(s)
Decodes a text string (see PDF 1.7 section 7.9.2.2 - it could
be PDFDocEncoding or UTF-16BE) to a str.
Source code in playa/utils.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
drange(v0, v1, d)
Returns a discrete range.
Source code in playa/miner.py
150 151 152 | |
extract(path, laparams=None, max_workers=1, mp_context=None)
Extract LTPages from a document.
Source code in playa/miner.py
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 | |
extract_page(page, laparams=None)
Extract an LTPage from a Page, and possibly do some layout analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
a Page as returned by PLAYA (please create this with space="page" if you want pdfminer.six compatibility). |
required |
laparams
|
Union[LAParams, None]
|
if None, no layout analysis is done. Otherwise do some kind of heuristic magic that all "Artificial Intelligence" depends on but nobody actually understands. |
None
|
Returns:
| Type | Description |
|---|---|
LTPage
|
An analysis of the page as |
Source code in playa/miner.py
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 | |
fsplit(pred, objs)
Split a list into two classes according to the predicate.
Source code in playa/miner.py
138 139 140 141 142 143 144 145 146 147 | |
make_path_segment(op, points)
Create a type-safe PathSegment, unlike pdfminer.six.
Source code in playa/miner.py
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 | |
process_object(obj)
Handle obj according to its type
Source code in playa/miner.py
1223 1224 1225 1226 | |
resolve1(x, default=None)
Resolves an object.
If this is an array or dictionary, it may still contains some indirect objects inside.
Source code in playa/pdftypes.py
225 226 227 228 229 230 231 232 233 | |
resolve_all(x, default=None)
Resolves all indirect object references inside the given object.
This creates new copies of any lists or dictionaries, so the original object is not modified. However, it will ultimately create circular references if they exist, so beware.
Source code in playa/pdftypes.py
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 | |
subpaths(path)
Iterate over "subpaths".
Note: subpaths inherit the values of fill and evenodd from
the parent path, but these values are no longer meaningful
since the winding rules must be applied to the composite path
as a whole (this is not a bug, just don't rely on them to know
which regions are filled or not).
Source code in playa/miner.py
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | |
uniq(objs)
Eliminates duplicated elements.
Source code in playa/miner.py
125 126 127 128 129 130 131 132 133 134 135 | |