将PDF文件转换为TXT文件

时间:2019-09-05 17:52:48

标签: python pandas pdf operating-system pypdf2

我需要专家的最后帮助!!我想将目录中的所有pdf文件转换为txt文件。我编写了一个代码来创建与pdf文件同名的空txt文件,并编写了将单个pdf转换为txt的代码,但是我想转换目录中的所有文件。请参见下面的代码: PS:我已经尝试使用PDFminer和其他所有软件包,但它不起作用

import pandas as pd
import os
import PyPDF2

###Create empty txt files Named as pdf files ###########

path = '....\\PDF2Text\\PDF\\'
newpath = '....\\PDF2Text\\Text\\'

files = []
for r, d, f in os.walk(path):
    for file in f:
        if '.pdf' in file:
            files.append(os.path.join(r, file))

for f in files:

    ext = f.replace('.pdf','.txt')
    extpath = ext.replace(path,newpath) 

    ft= open(extpath ,"w+")
    ft.close()
    print(extpath)   

 ##Here we Convert a single pdf file to a txt file providing pdf path and empty txt path #####

import PyPDF2

def getPDFFileContentToTXT(pdfFile):
    myPDFFile = PyPDF2.PdfFileReader(pdfFile)

    with open('....\\PDF2Text\\Text\\blabla.txt', 'w') as pdf_output:
       for page in range (myPDFFile.getNumPages()):
           data = myPDFFile.getPage(page).extractText()
           pdf_output.write(data)



   with open('.....\\PDF2Text\\Text\\blabla.txt', 'r') as myPDFContent:
       return myPDFContent.read().replace('\n',' ')

pdfFileContent = getPDFFileContentToTXT('.....\\PDF2Text\\PDF\\blabla.pdf')

2 个答案:

答案 0 :(得分:0)

您尝试过提卡吗?只需执行pip install tika(还需要在系统上安装Java 7+),也许这就是您想要的代码段:

import os
from tika import parser

def read_pdf(pdf_file):

    text = parser.from_file(pdf_file)['content']
    return text.encode('utf-8')

def pdf_to_txt(folder_with_pdf, dest_folder):

"""

folder_with_pdf: path to your pdf's
dest_folder: path where you want .txt files saved

"""

    pdf_files = []

    for root, dirs, files in os.walk(folder_with_pdf):
        for f in files:
            if '.pdf' in f:
                pdf_files.append(os.path.join(root, f))
    #print(pdf_files)

    for file_ in pdf_files:
        text_file = os.path.splitext(os.path.basename(file_))[0]+'.txt'
        with open(os.path.join(dest_folder,text_file), 'wb') as text_f:
            text_f.write(read_pdf(file_))

    return None

pdf_to_txt('./pdf_folder', './txt_folder') #you should see .txt files being populated in ./txt_folder

此外:如果./pdf_folder子目录中的pdf文件碰巧具有相同的名称(但内容不同),那么您将丢失一个(或多个).txt文件。

答案 1 :(得分:0)

import pandas as pd
import os
import PyPDF2

#Create empty txt files Named as pdf files 

path = 'C:\\PDF2Text\\PDF\\'
newpath = 'C:\\PDF2Text\\Text\\'

# r=root, d=directories, f = files

files = []
for r, d, f in os.walk(path):
    for file in f:
        if '.pdf' in file:
            files.append(os.path.join(r, file))

for f in files:

    txt = f.replace('.pdf','.txt')
    txtpath = txt.replace(path,newpath) 
    print(f)
    ft= open(txtpath ,"w+")
    ft.close()
    print(txtpath)
    Vpath = f.replace('.pdf','')
    #print(Vpath)

    myPDFFile = PyPDF2.PdfFileReader(f)
    with open(txtpath, 'w') as pdf_output:   #, encoding="utf-8"
         for page in range (myPDFFile.getNumPages()):
            data = myPDFFile.getPage(page).extractText()
            pdf_output.write(data)            
    with open(txtpath, 'r') as myPDFContent:
        myPDFContent.read().replace('\n',' ')
相关问题