乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python讀取pdf中的文本

       老三的休閑書屋 2021-04-10

      python處理pdf也是常用的技術(shù)了,對(duì)于python3來說,pdfminer3k是一個(gè)非常好的工具。

      pip install pdfminer3k

      首先,為了滿足大部分人的需求,我先給一個(gè)通用一點(diǎn)的腳本來讀取pdf中的文本:

      1. from io import StringIO
      2. from io import open
      3. from pdfminer.converter import TextConverter
      4. from pdfminer.layout import LAParams
      5. from pdfminer.pdfinterp import PDFResourceManager, process_pdf
      6. def read_pdf(pdf):
      7. # resource manager
      8. rsrcmgr = PDFResourceManager()
      9. retstr = StringIO()
      10. laparams = LAParams()
      11. # device
      12. device = TextConverter(rsrcmgr, retstr, laparams=laparams)
      13. process_pdf(rsrcmgr, device, pdf)
      14. device.close()
      15. content = retstr.getvalue()
      16. retstr.close()
      17. # 獲取所有行
      18. lines = str(content).split('\n')
      19. return lines
      20. if __name__ == '__main__':
      21. with open('t1.pdf', 'rb') as my_pdf:
      22. print(read_pdf(my_pdf))

      我主要是想在pdf中抽出自己想要的一些關(guān)鍵信息,所以需要找到這些信息的共同點(diǎn)。幸運(yùn)的是,這些關(guān)鍵信息的行都含有'//',所以我只需找到含有'//'的行就行了,于是寫了以下腳本。

      這樣就可以直接使用了,我們先看腳本:

      1. from io import StringIO
      2. from io import open
      3. from pdfminer.converter import TextConverter
      4. from pdfminer.layout import LAParams
      5. from pdfminer.pdfinterp import PDFResourceManager, process_pdf
      6. def read_pdf(pdf):
      7. # resource manager
      8. rsrcmgr = PDFResourceManager()
      9. retstr = StringIO()
      10. laparams = LAParams()
      11. # device
      12. device = TextConverter(rsrcmgr, retstr, laparams=laparams)
      13. process_pdf(rsrcmgr, device, pdf)
      14. device.close()
      15. content = retstr.getvalue()
      16. retstr.close()
      17. # 獲取所有行
      18. lines = str(content).split('\n')
      19. units = [1, 2, 3, 5, 7, 8, 9, 11, 12, 13]
      20. header = '\x0cUNIT '
      21. # print(lines[0:100])
      22. count = 0
      23. flag = False
      24. text = open('words.txt', 'w+')
      25. for line in lines:
      26. if line.startswith(header):
      27. flag = False
      28. count += 1
      29. if count in units:
      30. flag = True
      31. print(line)
      32. text.writelines(line + '\n')
      33. if '//' in line and flag:
      34. text_line = line.split('//')[0].split('. ')[-1]
      35. print(text_line)
      36. text.writelines(text_line+'\n')
      37. text.close()
      38. def _main():
      39. my_pdf = open('t1.pdf', 'rb')
      40. read_pdf(my_pdf)
      41. my_pdf.close()
      42. if __name__ == '__main__':
      43. _main()

      其實(shí)看到lines =  str(content).split('\n')那一行就夠了,我們可以把lines都print出來,就可以看到pdf里面的內(nèi)容。

      這樣我們就可以把pdf文件處理看作簡(jiǎn)單的字符串?dāng)?shù)據(jù)處理了。接下來的腳本操作也不用過多解釋了。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多