从PDF文件中提取文本:有时会忽略重要标记

时间:2018-02-18 04:19:11

标签: python pdf text-extraction pypdf

我正在处理WTO小组报告文件,下面使用的pdf链接在这里:https://docs.wto.org/dol2fe/Pages/FE_Search/FE_S_S006.aspx?Query=(%40Symbol%3d+wt%2fds2%2f *)& Language = ENGLISH& Context = FomerScriptedSearch& languageUIChanged = true#

如果我使用PyPDF2:

import PyPDF2
pdf_file = open('/Users/zachary/Downloads/2R.pdf', 'rb')
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page = read_pdf.getPage(1)
page_content = page.extractText()
print(page_content)

它返回了良好的结果,但在单词之间没有空格

TableofContentsI.INTRODUCTION .................................... 1II.FACTUALASPECTS .. .................................. 2A.TheCleanAirAct ............. ..................... 3B.EPA' sGasolineRule ...................... .......... 31.EstablishmentofBaselines ........................ 32.ReformulatedGasoline .......... ................ 43.ConventionalGasoline(或"反DumpingRules&#34)........ 4C.TheMay1994Proposal ........ ...................... 5III.MAINARGUMENTS ......................... ........... 5A.General .................................... 5B.TheGeneralAgreementonTariffsandTrade ................ 61.ArticleI-GeneralMost惠-NationTreatment ....... 62.ArticleIII,NationalTreatmentonInternalTaxationandRegulation .......... ..................... 7A)ArticleIII:4 ........................ .... 7B)ArticleIII:1 ............................ 143.ArticleXX-GeneralExceptions ........ ............ 154.ArticleXX(b)................................图15A)" ProtectionofHuman,AnimalandPlantLifeorHealth" .... ......................... 15B)"必要" ............... ............. 155.ArticleXX(d)............................... .216.ArticleXX(克)................................ 22A)" Relatedtotheconservationofexhaustiblenaturalresources ...& #34; ............................ 22B)" ... madeeffectiveinconjunctionwithrestrictionsondomesticproductionorconsumption" ...... .... 237.PreambletoArticleXX .......................... 238.ArticleXXIII,NullificationandImpairment ............ 25

对我来说重要的是每个" Roman"角色(I.简介II。实际方面III.Main Arguments)位于目录'页。我将参考此页面标记来解析页面。

另外,如果我使用模块textract:

text = textract.process('/Users/zachary/Downloads/{}R.pdf'.format(i), method ='pdfminer' )#import text file

text = str(text)

text

b' WORLD TRADE \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n / n \ n \ n n时间\ n \ n n重新制定的\ n \ n和常规汽油的标准\ n \ n报告面板\ n \ n \ x0c目录\ n \ nI。\ n \ nII。\ n \ nINRODUCTION \ n \ n。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n实际方面。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ nA。\ n \ nB。\ n \ n“清洁空气法”。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ nEPA的汽油规则。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n1。\ n \ n2。\ n \ n3。\ n \ n建立基线。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n配方汽油。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n常规汽油(或"反倾销规则")\ n \ n。 。 。 。 。 。 。 。\ n \ nC。\ n \ n 1994年5月的提案\ n \ n。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ nIII。主要论点。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \钠。\ n \ NB。\ n \ nGeneral \ n \ n。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n关税与贸易总协定。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n1。\ n \ n2。\ n \ n3。\ n \ n4。\ n \ n5。\ n \ n6。\ n \ n7。\ n \ n8。\ n \ n \ n第一条 - 一般情况 - 优惠待遇\ n \ n。 。 。 。 。 。 。\ n \ n第三条 - 国内对内部税收和监管的处理。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ na)\ n \ nb)\ n \ n第三条:4。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n第三条:1。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n第XX条 - 一般例外。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。\ n \ n文章XX(b)\ n \ n。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。 。

正如你在textract上看到的那样,没有II。在事实方面。

它刚刚消失!

我可以做些什么来解决这个问题并实现两个目标 - 1)让罗马标记生存2)但仍然可以很好地插入白色空间。

请给我一些宝贵的建议继续。

THX

1 个答案:

答案 0 :(得分:0)

如果您愿意考虑PyPDF以外的解决方案,请查看pdftotext。与PyPDF相比,我对pdftotext的运气更好。您也可以尝试使用PDFMiner但我已经得到了混合结果。

如果您愿意付费,ABBYY会为此制作好的软件,但价格昂贵。

相关问题