怎么提取pdf格式中的英语单词

思路

第一步:适用python把需要导出的pdf文件单词导出到txt

第二步:把导出的txt导入到软件单词库,例如,金山词霸等软件内

第三步:熟练掌握以及删除单词库部分单词,达到对英文标准的单词记忆,方便理解专业信息。

以下代码演示如何将py当前目录下的Workspace子目录里的PDF里的英语单词提取出来。

python 复制代码
import pdfplumber
import glob,os

WordDict = dict()

def isWord(word):
    retVal = True
    if len(word) < 5 or word.isidentifier() == False or word.isascii() == False:
        retVal = False
    else:
        for c in word:
            if c in ['0','1', '2', '3', '4', '5', '6', '7', '8', '9', '_']:
                retVal = False
    return retVal

#DIR=r"E:\GetEnglishDictionary"
DIR = os.getcwd() + "\\workspace\\"
temp=os.listdir(DIR)
Dirlist=[]
for i in temp:
    if (i.find(".pdf"))!= -1:
        Dirlist.append(i)

try:
    #  out
    for dir in Dirlist:
        print ("Analyse {} file".format(dir))
        #file=glob.glob(os.path.join(DIR+"\\"+dir, "*.*"))
        pdffile = DIR + "\\" + dir

        wordDictFile = pdffile.replace(".pdf", "_dict") + ".txt"
        dictFile = open(wordDictFile, 'w', encoding="utf-8")

        with pdfplumber.open(pdffile) as pdf:

            #for j in range(1, 2):
            pageNum = len(pdf.pages)
            progress = 0
            now = 0
            pageIndex = 0
            for page in pdf.pages:
                pageIndex = pageIndex + 1
                progress = int(pageIndex * 100/ pageNum)
                if progress >= now + 1:
                    print(pdffile + " : " + str(progress) + " %")
                    now = progress


                # 读取PDF文档第i+1页
                #page = pdf.pages[j]
                # page.extract_text()函数即读取文本内容
                txt = page.extract_text()
                txt = txt.replace(',', ' ')
                txt = txt.replace('\n', ' ')
                #words = ''.join(txt.split('\n')[:-1])
                #vols = str(words).split(' ')
                vols = str(txt).split(' ')
                for vol in vols:
                    if isWord(vol) == True:
                        #print(vol)
                        tst = WordDict.get(vol.capitalize())
                        if tst == None:
                            WordDict[vol.capitalize()] = vol
                            dictFile.write( vol + "\n")
                            #str(pageIndex) + " " +
                ##
        dictFile.close()
        print("共 " + str(pageNum) + " 页,提取单词:" + str(len(WordDict)) + " 个")


except Exception as e :
    print(repr(e))

finally:
    print("finish write")
相关推荐
开发者小天17 分钟前
python中的class类
开发语言·python
idwangzhen26 分钟前
GEO优化系统哪家更专业
python·信息可视化
diediedei44 分钟前
机器学习模型部署:将模型转化为Web API
jvm·数据库·python
m0_561359671 小时前
使用Python自动收发邮件
jvm·数据库·python
naruto_lnq1 小时前
用Python批量处理Excel和CSV文件
jvm·数据库·python
b2077211 小时前
Flutter for OpenHarmony 身体健康状况记录App实战 - 提醒设置实现
python·flutter·macos·cocoa·harmonyos
2301_822365031 小时前
数据分析与科学计算
jvm·数据库·python
河北小博博1 小时前
分布式系统稳定性基石:熔断与限流的深度解析(附Python实战)
java·开发语言·python
黄连升2 小时前
Python学习第二天,系统学习基础
python·学习
西红市杰出青年2 小时前
CSS 选择器详细教程:原理、语法、方向/“轴”与实战
css·python