本文實例為大家分享了Python批量提取PDF文件中文本的具體代碼,供大家參考,具體內容如下
首先需要執行命令pip install pdfminer3k來安裝處理PDF文件的擴展庫。
import osimport sysimport timepdfs = (pdfs for pdfs in os.listdir('.') if pdfs.endswith('.pdf'))for pdf1 in pdfs: pdf = pdf1.replace(' ', '_').replace('-', '_').replace('&', '_') os.rename(pdf1, pdf) print('='*30) print(pdf) txt = pdf[:-4] + '.txt' exe = '"' + sys.executable + '" "' pdf2txt = os.path.dirname(sys.executable) pdf2txt = pdf2txt + '//scripts//pdf2txt.py" -o ' try: #調用命令行工具pdf2txt.py進行轉換 #如果pdf加密過可以改寫下面的代碼 #在-o前面使用-P來指定密碼 cmd = exe + pdf2txt + txt + ' ' + pdf os.popen(cmd) #轉換需要一定時間,一般小文件2秒鐘足夠了 time.sleep(2) #輸出轉換后的文本,前200個字符 with open(txt, encoding='utf8') as fp: print(fp.read(200)) except: pass來源:python小屋
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答