怎么提取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")
相关推荐
程序员阿龙8 分钟前
【精选】计算机毕业设计Python Flask海口天气数据分析可视化系统 气象数据采集处理 天气趋势图表展示 数据可视化平台源码+论文+PPT+讲解
python·flask·课程设计·数据可视化系统·天气数据分析·海口气象数据·pandas 数据处理
ZHOU_WUYI14 分钟前
Flask与Celery 项目应用(shared_task使用)
后端·python·flask
且慢.58932 分钟前
Python_day47
python·深度学习·计算机视觉
佩奇的技术笔记40 分钟前
Python入门手册:异常处理
python
大写-凌祁1 小时前
论文阅读:HySCDG生成式数据处理流程
论文阅读·人工智能·笔记·python·机器学习
爱喝喜茶爱吃烤冷面的小黑黑1 小时前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
Blossom.1182 小时前
使用Python和Flask构建简单的机器学习API
人工智能·python·深度学习·目标检测·机器学习·数据挖掘·flask
Love__Tay3 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
有风南来4 小时前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin1804 小时前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python