问题
I am trying to make a primitive crawler for my own pdf files. For that, I use Pypdf
to extract the Data (Customer, Product, Amount, etc.) and use that data.
Now, I have the code, its pretty easy, but it doesn't seem to be able to extract anything out of my PDFs while I tried it on some random PDF from google and it works. I tried with multiple of my documents, pdfs, don't work, random pdf off the internet works.
I use Spyder.
Below is the code I am using:
import PyPDF2 as p2
PDFfile=open("pdf_barrierefrei.pdf","rb") # Random PFD off the Internet
pdfread = p2.PdfFileReader(PDFfile)
x = pdfread.getPage(0)
print(x.extractText())
PDFfile.close
PDFfile=open("2.pdf","rb")
pdfread = p2.PdfFileReader(PDFfile) # My PDF
y = pdfread.getPage(0)
print(y.extractText())
PDFfile.close
My Expected output is a string of mashed-up data that is part of my PDF, like customer, My name, date, etc which I then sort and clear out.
The actual result is either:
" [b'\n', b'endobj\n', b'11 0 obj\n', b'<<\n', b'/Length 1011\n', b'>>\n', b'stream\n', b'/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def 1 begincodespacerange <0000> endcodespacerange 49 beginbfchar <0003> <0020> <0004> <0041> <0011> <0042> <0012> <0043> <001C> <0045> <0026> <0046> <0027> <0047> <002C> <0048> <002F> <0049> <003E> <004C> <0045> <004E> <004B> <004F> <0057> <0050> <005A> <0052> <005E> <0053> <0064> <0054> <0068> <0055> <0073> <0056> <0102> <0061> <010F> <0062> <0110> <0063> <011A> <0064> <011E> <0065> <0128> <0066> <0150> <0067> <015A> <0068> <015D> <0069> <016C> <006B> <0175> <006D> <0176> <006E> <017D> <006F> <018C> <0072> <0190> <0073> <019A> <0074> <01B5> <0075> <0355> <002C> <0357> <003A> <0358> <002E> <036C> <002F> <0372> <002D> <03A6> <20AC> <03EC> <0030> <03ED> <0031> <03EE> <0032> <03F0> <0034> <03F1> <0035> <03F2> <0036> <03F3> <0037> <03F5> <0039> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end \n', b'endstream\n', b'endobj\n', b'10 0 obj\n', b'[ 3 3 226 4 4 605 17 17 560 18 18 529 28 28 487 38 38 458 39 39 637 44 44 630 47 47 266 62 62 422 69 69 658 75 75 676 87 87 532 90 90 562 94 94 472 100 100 495 104 104 652 115 115 591 258 258 493 271 271 536 272 272 418 282 282 536 286 286 503 296 296 316 336 336 474 346 346 536 349 349 245 364 364 479 373 373 813 374 374 536 381 381 537 396 396 355 400 400 398 410 410 346 437 437 536 853 853 257 855 855 275 856 856 267 876 876 429 882 882 306 934 934 506 1004 1004 506 1005 1005 506 1006 1006 506 1008 1008 506 1009 1009 506 1010 1010 506 1011 1011 506 1013 1013 506 ]\n', b'endobj\n', b'7 0 obj\n', b'[ -813 -268 813 952 ]\n', b'endobj\n', b'8 0 obj\n', b'813\n', b'endobj\n', b'19 0 obj\n', b'<<\n', b'/Length 1207\n', b'>>\n', b'stream\n', b'/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def 1 begincodespacerange <0000> endcodespacerange 63 beginbfchar <0003> <0020> <0004> <0041> <0011> <0042> <0012> <0043> <0018> <0044> <0026> <0046> <0027> <0047> <002C> <0048> <002F> <0049> <003C> <004B> <003E> <004C> <0044> <004D> <0045> <004E> <004B> <004F> <0057> <0050> <005A> <0052> <005E> <0053> <0064> <0054> <0068> <0055> <0073> <0056> <0074> <0057> <007F> <005A> <0102> <0061> <010F> <0062> <0110> <0063> <011A> <0064> <011E> <0065> <0128> <0066> <0150> <0067> <015A> <0068> <015D> <0069> <016C> <006B> <016F> <006C> <0175> <006D> <0176> <006E> <017D> <006F> <0189> <0070> <018C> <0072> <0190> <0073> <0198> <00DF> <019A> <0074> <01B5> <0075> <01C0> <0076> <01C1> <0077> <01CC> <007A> <0355> <002C> <0358> <002E> <036C> <002F> <0372> <002D> <039B> <0040> <03A6> <20AC> <03EC> <0030> <03ED> <0031> <03EE> <0032> <03EF> <0033> <03F0> <0034> <03F1> <0035> <03F2> <0036> <03F3> <0037> <03F4> <0038> <03F5> <0039> <0439> <0025> <043D> <002B> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end \n', b'endstream\n', b'endobj\n', b'18 0 obj\n', b'[ 3 3 226 4 4 578 17 17 543 18 18 533 24 24 615 38 38 459 39 39 630 44 44 623 47 47 251 60 60 519 62 62 420 68 68 854 69 69 645 75 75 662 87 87 516 90 90 542 94 94 459 100 100 487 104 104 641 115 115 567 116 116 889 127 127 468 258 258 479 271 271 525 272 272 422 282 282 525 286 286 497 296 296 305 336 336 470 346 346 525 349 349 229 364 364 454 367 367 229 373 373 798 374 374 525 381 381 527 393 393 525 396 396 348 400 400 391 408 408 527 410 410 334 437 437 525 448 448 451 449 449 714 460 460 395 853 853 249 856 856 252 876 876 386 882 882 306 923 923 894 934 934 506 1004 1004 506 1005 1005 506 1006 1006 506 1007 1007 506 1008 1008 506 1009 1009 506 1010 1010 506 1011 1011 506 1012 1012 506 1013 1013 506 1081 1081 714 1085 1085 498 ]\n', b'endobj\n', b'15 0 obj\n', b'[ -894 -268 894 952 ]\n', b'endobj\n', b'16 0 obj\n', b'894\n', b'endobj\n', b'2 0 obj\n', b'<<\n', b'/Count 1\n', b'/Kids [ 3 0 R ]\n', b'/Type /Pages\n', b'>>\n', b'endobj\n', b'1 0 obj\n', b'<<\n', b'/Pages 2 0 R\n', b'/Type /Catalog\n', b'>>\n', b'endobj\n', b'23 0 obj\n', b'<<\n', b'/Author (Schicki)\n', b"/CreationDate (D:20190913102353+02'00')\n", b"/ModDate (D:20190913102353+02'00')\n", b'/Producer (Microsoft: Print To PDF)\n', b'/Title (Produktliste Schickmaier Excel.xlsx)\n', b'>>\n', b'endobj\n', b'xref\n', b'0 24\r\n', b'0000000000 65535 f\r\n', b'0000585703 00000 n\r\n', b'0000585644 00000 n\r\n', b'0000581675 00000 n\r\n', b'0000000009 00000 n\r\n', b'0000148586 00000 n\r\n', b'0000148612 00000 n\r\n', b'0000583508 00000 n\r\n', b'0000583545 00000 n\r\n', b'0000148635 00000 n\r\n', b'0000582918 00000 n\r\n', b'0000581854 00000 n\r\n', b'0000355352 00000 n\r\n', b'0000355825 00000 n\r\n', b'0000355852 00000 n\r\n', b'0000585586 00000 n\r\n', b'0000585624 00000 n\r\n', b'0000355876 00000 n\r\n', b'0000584824 00000 n\r\n', b'0000583564 00000 n\r\n', b'0000577725 00000 n\r\n', b'0000578203 00000 n\r\n', b'0000581590 00000 n\r\n', b'0000585752 00000 n\r\n', b'trailer\n', b'<<\n', b'/Info 23 0 R\n', b'/Root 1 0 R\n', b'/Size 24\n', b'>>\n', b'startxref\n', b'585949\n', b'%%EOF\n']"
or half a page of nothing, just "In [40]:" and then literally 20 sentences of nothing, no error or anything
The output got from the random Pdf:
"Aktion Mensch e.V. Fachartikel —PDF-Dokume....
16.10.03 Autor: Roland Heuwinkel 17. Oktober 2003 Seite 1 von 24 "
It is just some random scribbly, don't care about the content, just the fact that it gets me some text.
I am really new to coding, and I have no idea where to start, as both of them are PDFs. I am just wondering if it is because I used "Print to PDF" to create them?
回答1:
For pdf_barrierefrei.pdf, looks like the text result is converted into unicode format. By the way, I'm using jupyther notebook and not spyder, so I'm getting correct result.
For 2.pdf, the pdf has an image logo and table. Install tika (pip install tika) then use the code below to read the non-images into text.
from tika import parser
raw = parser.from_file('2.pdf')
print(raw['content'])
Result:
Produktliste Schickmaier Excel.xlsx
LIEFERSCHEIN
Kunde Customer Adresse Adress
Adress Data Data
K/DB-Nr. 211 Contact
Preis/N M Gesamtpreis
Bio Erdbeer-Chilischokolade 3,05 € 20 61,09 €
Bio Beuscherl 5,23 € 6 31,36 €
Bio ChiliconCarne 5,98 € 15 89,77 €
Bio Geschnetzeltes 5,23 € 15 78,41 €
Versand Brutto Versand Netto - €
Warenwert netto 10% 260,64 €
Umsatzsteuer 10% 26,06 €
RECHNUNGSBETRAG BRUTTO 286,70 €
Seite 1/1
2019/
Data
来源:https://stackoverflow.com/questions/58768571/pypdf-extracts-code-from-one-pdf-but-not-from-another