将PDF文件转换为TXT文件 [英] Convert a PDF files to TXT files

查看:107
本文介绍了将PDF文件转换为TXT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a last touch from an expert !! I want to convert all pdf files in a directory to txt files. I wrote a code to create empty txt files having the same name as pdf files and a code to convert a single pdf to txt but I want to convert all files in the directory. please see the code below: PS : I Already tried with PDFminer, and every other package and it does not work

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

推荐答案

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

这篇关于将PDF文件转换为TXT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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