pdfconverter
Convert pdf files to the Giella xml format.
PDF2XMLConverter
Bases: basicconverter.BasicConverter
Class to convert the xml output of pdftohtml to Giella xml.
Attributes:
Name | Type | Description |
---|---|---|
extractor |
PDFTextExtractor
|
class to extract text from the xml that pdftohtml produces. |
pdffontspecs |
PDFFontspecs
|
class to store fontspecs found in the xml pages. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
__init__(filename)
Initialise the PDF2XMLConverte class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
the path to the pdf file. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
446 447 448 449 450 451 452 453 |
|
add_fontspecs(page)
Extract font specs found in a pdf2xml page element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page |
etree.Element
|
a pdf page |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
614 615 616 617 618 619 620 621 |
|
convert2intermediate()
Convert from pdf to a corpus xml file.
Returns:
Type | Description |
---|---|
lxml.etree.Element
|
A corpus xml etree with the content of the pdf file, but without most of the metadata. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
514 515 516 517 518 519 520 521 522 523 |
|
extract_text(command)
Extract the text from a document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command |
list[str]
|
a list containing the command and the arguments sent to ExternalCommandRunner. |
required |
Returns:
Type | Description |
---|---|
bytes
|
byte string containing the output of the program |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
|
handle_syntaxerror(error, lineno, invalid_input)
Handle an xml syntax error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error |
Exception
|
an exception |
required |
lineno |
int
|
the line number in this module where the error happened. |
required |
invalid_input |
str
|
a string containing the invalid input. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
|
parse_page(page)
Parse the page element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page |
Any
|
a pdf xml page element. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
parse_pages(root_element)
Parse the pages of the pdf xml document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_element |
xml.etree.Element
|
the root element of the pdf2xml document. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
601 602 603 604 605 606 607 608 609 610 611 612 |
|
pdftohtml2html(pdftohtmloutput)
Convert output of pdftohtml to html (applying our regular fixes)
Returns:
Type | Description |
---|---|
str
|
An html file as string with the content of the pdf file, but without most of the metadata. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
pdftohtml2intermediate(pdftohtmloutput)
Convert output of pdftohtml to a corpus xml file.
Returns:
Type | Description |
---|---|
lxml.etree.Element
|
A corpus xml etree with the content of the pdf file, but without most of the metadata. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
replace_ligatures(content)
staticmethod
Replace unwanted strings with correct replacements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
content of an xml document. |
required |
Returns:
Type | Description |
---|---|
str
|
String containing the new content of the xml document. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
strip_chars(content, extra='')
staticmethod
Strip unwanted chars from the document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
the xml document that pdftohtml produces |
required |
extra |
str
|
more character that should be removed |
''
|
Returns:
Type | Description |
---|---|
str
|
containing the modified version of the document. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
|
PDFEmptyPageError
Bases: Exception
Raise this exception if a pdf page is empty.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
162 163 |
|
PDFFontspecs
Add font specs found in a pdf page to this class.
Attributes:
Name | Type | Description |
---|---|---|
pdffontspecs |
dict[PDFFontspec, int]
|
map fontspecs to fontspec ids. |
duplicates |
dict[str, str]
|
map ids of duplicate fontspecs to the id of the first instance of this fontspec. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
__init__()
Initialise the PDFFontspecs class.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
118 119 120 121 |
|
add_fontspec(xmlfontspec)
Add a pdf2xml fontspec to this class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xmlfontspec |
etree.Element
|
a PDF2XML fontspec element found in a PDF2XML page element. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
corrected_id(font_id)
Return a corrected id of a fontspec.
Some xmlfontspecs have different id's for an identical font. This function makes sure identical fonts have identical id's.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
font_id |
int
|
an integer that is the id of the fontspec. |
required |
Returns:
Type | Description |
---|---|
int
|
an integer that is the corrected id of the fontspec. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
PDFPage
Reads a page element.
Attributes:
Name | Type | Description |
---|---|---|
textelements |
list of PDFTextElements
|
contains the text of the page |
pdf_pagemetadata |
PDFPageMetadata
|
contains the metadata of the page |
The textelements are manipulated in several ways, then ordered in the way they appear on the page and finally sent to PDFTextExtractor
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
linespacing
property
Return linespacing.
__init__(page_element, metadata_margins=None, metadata_inner_margins=None, linespacing=None)
Initialise the PDFPage class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page_element |
etree.Element
|
an etree element representing a pdf page |
required |
metadata_margins |
dict
|
a dict containing margins read from the metadata file. |
None
|
metadata_inner_margins |
dict
|
a dict containing inner_margins read from the metadata file. |
None
|
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
fix_font_id(pdffontspecs)
Fix font id in text elements.
Sometimes the same font has different ID's. Correct that ID if necessary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdffontspecs |
PDFFontspecs
|
a PDFFontspecs instance. |
required |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
376 377 378 379 380 381 382 383 384 385 386 387 |
|
is_inside_margins(text, margins)
staticmethod
Check if t is inside the given margins.
t is a text element
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
is_skip_page(skip_pages)
Found out if this page should be skipped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
skip_pages |
list of mixed
|
list of the pages that should be skipped. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if this page should be skipped, otherwise false. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
pick_valid_text_elements()
Pick the wanted text elements from a page.
This is the main function of this class
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
422 423 424 425 426 427 428 429 430 431 432 433 |
|
remove_elements_outside_margin()
Remove PDFTextElements from textelements if needed.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
PDFPageMetadata
Read pdf metadata from the metadata file into this class.
Compute metadata needed by the conversion from the data contained in this class.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
__init__(page_id, page_style, metadata_margins=None, metadata_inner_margins=None)
Initialise the PDFPageMetadata class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page_id |
str
|
the page id |
required |
page_style |
str
|
the styles as a css string |
required |
metadata_margins |
dict
|
a dict containing margins read from the metadata file. |
None
|
metadata_inner_margins |
dict
|
a dict containing inner_margins read from the metadata file. |
None
|
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
compute_inner_margin(margin)
Compute a margin in pixels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
margin |
str
|
the name of the margin |
required |
Returns:
Type | Description |
---|---|
int
|
an int telling where the margin is on the page. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
compute_inner_margins()
Compute inner margins of the document.
Returns:
Type | Description |
---|---|
dict
|
A dict where the key is the name of the margin and the value is an integer indicating where the margin is on the page. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
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 |
|
compute_margin(margin)
Compute a margin in pixels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
margin |
str
|
the name of the margin |
required |
Returns:
Type | Description |
---|---|
int
|
an int telling where the margin is on the page. |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
compute_margins()
Compute the margins of a page in pixels.
Returns:
Type | Description |
---|---|
dict
|
a dict containing the four margins in pixels |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
193 194 195 196 197 198 199 200 201 202 203 204 |
|
get_coefficient(margin)
Get the width of the margin in percent.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
get_inner_coefficient(margin)
Get the width of the margin in percent.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
handle_br(previous, current)
Handle br tags in p elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
previous |
str
|
the previous string in front of a particular br tag |
required |
current |
str
|
the current string following a particular br tag |
required |
Returns:
Type | Description |
---|---|
str
|
A possibly modified version of previous |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
is_probably_hyphenated(previous, current)
Find out if previous is part of a hyphenated word.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
previous |
str
|
the previous string in front of a particular br tag |
required |
current |
str
|
the current string following a particular br tag |
required |
Returns:
Type | Description |
---|---|
bool
|
True if previous is part of a hyphenated word, False otherwise |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
merge(first, second)
Merge two paragraph elements into one.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
styles(page_style)
Turn inline css styles into a dict.
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
32 33 34 35 36 37 38 39 40 |
|
to_html_elt(path)
Convert a pdf document to the Giella xml format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
path to the document |
required |
Returns:
Type | Description |
---|---|
lxml.etree.Element
|
the root element of the Giella xml document |
Source code in /home/anders/projects/CorpusTools/corpustools/pdfconverter.py
687 688 689 690 691 692 693 694 695 696 697 |
|