Pypdf从一个PDF提取代码,而不从另一个PDF提取代码? [英] Pypdf extracts code from one PDF, but not from another?

查看:75
本文介绍了Pypdf从一个PDF提取代码,而不从另一个PDF提取代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自己的pdf文件制作原始的搜寻器.为此,我使用Pypdf提取数据(客户,产品,金额等)并使用该数据.

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.

现在,我有了代码,它很简单,但是当我尝试从Google随机提取一些PDF时,它似乎无法从我的PDF中提取任何内容,并且可以正常工作.我尝试了多个文档,pdf,不起作用,从互联网上随机抽取pdf.

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.

我使用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

我的预期输出是一串混搭的数据,这些数据是我的PDF的一部分,例如客户,我的姓名,日期等,然后我对其进行排序和清除.

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.

实际结果是:

" [b'\ n',b'endobj \ n',b'11 0 obj \ n',b'<< \ n',b'/长度1011 \ n', b'>> \ n',b'stream \ n',b'/CIDInit/ProcSet findresource开始12 dict 开始begincmap/CIDSystemInfo<< /注册(Adobe)/订购(UCS) /补充0 >> def/CMapName/Adob​​e-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 100100 495 104 104 652 115 115 591 258 258 493 271 271 536 272 272 418 282 282 536 286286503296296296316336336474346346536536349349245364364364 479 373 373 813 813 374 374 536 381 381 537 396 396 355 400 400 398 410 410346437437536536853853257855855275856856856267876876429 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'/长度 1207 \ n',b'>> \ n',b'stream \ n',b'/CIDInit/ProcSet findresource开始 12 dict开始begincmap/CIDSystemInfo<< /注册(Adobe)/订购 (UCS)/补充0 >> def/CMapName/Adob​​e-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 100100 487 104 104 641 115 115 567 116 116 889 127 127 468 258 258479271271525525272272422282282525286286286296296296305 336 336 470 346 346 525 349 349 229 364 364 454 367 367 229 373 373 798374374374525381381527393393393525396396348400400391408 408527410410410334437437525448448451451449449714460460395 853853249249856856252876876386882882306923923894934934 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 894952] \ 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'/类型/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'/作者(Schicki)\ n',b"/CreationDate (D:20190913102353 + 02'00')\ n,b"/ModDate(D:20190913102353 + 02'00')\ n, b'/生产者(Microsoft:打印到PDF)\ n',b'/标题(产品列表) 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'] "

" [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']"

半页什么都没有,只有在[40]中:",然后是20个句子,什么都没有,没有错误或其他任何内容

or half a page of nothing, just "In [40]:" and then literally 20 sentences of nothing, no error or anything

输出来自随机Pdf:

" Aktion Mensch e.V. Fachartikel —PDF-Dokume....

"Aktion Mensch e.V. Fachartikel —PDF-Dokume....

16.10.03自动:罗兰·休温克尔(Roland Heuwinkel)17. 2003年10月Seite 1 von 24 "

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.

我真的是编码新手,我也不知道从哪里开始,因为它们都是PDF.我只是想知道是否是因为我使用打印到PDF"来创建它们?

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?

推荐答案

对于pdf_barrierefrei.pdf,看起来好像文本结果已转换为Unicode格式.顺便说一句,我使用的是jupyther笔记本而不是spyder,所以我得到了正确的结果.

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.

对于2.pdf,pdf带有图像徽标和表格.安装tika(pip install tika),然后使用下面的代码将非图像读取为文本.

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'])

结果:

Schickmaier产品Excel.xlsx

Produktliste Schickmaier Excel.xlsx

LIEFERSCHEIN

LIEFERSCHEIN

客户群 地址Adresse地址

Kunde Customer Adresse Adress

地址数据 数据

K/DB-Nr. 211联系人

K/DB-Nr. 211 Contact

Preis/N M Gesamtpreis

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€

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-€

Versand Brutto Versand Netto - €

Warenwert净价10%260,64€
Umsatzsteuer 10%26,06€

Warenwert netto 10% 260,64 €
Umsatzsteuer 10% 26,06 €

RECHNUNGSBETRAG BRUTTO 286,70€
Seite 1/1

RECHNUNGSBETRAG BRUTTO 286,70 €
Seite 1/1

2019/

数据

这篇关于Pypdf从一个PDF提取代码,而不从另一个PDF提取代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆