怎么提取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")
相关推荐
keke.shengfengpolang1 分钟前
2026秋招信用风险分析岗面试:SQL和Python到底要学到什么程度才能过?
python
2601_9620990820 分钟前
python flask sqlalchemy连接数据库流程介绍
数据库·python·flask·教程·sqlalchemy
2601_9622932422 分钟前
python接口自动化(二十八)--html测试 报告——下(详解)
python·接口自动化·编码问题·htmltestrunner·报告优化
烂蜻蜓25 分钟前
Flask入门教程(附录):模板渲染API——Jinja2集成全解析
后端·python·flask
2601_9623878230 分钟前
Python CUDA 编程 - 2 - Numba 简介
python·numpy·cuda·jit编译器·numba
智能体与具身智能9 小时前
TVA具身智能的概念、架构与应用(19)
人工智能·python·具身智能
2601_9622946110 小时前
python中range函数怎么用
python·for循环·可迭代对象·range函数·整数列表
weixin_4692738110 小时前
如何在线把文字转为 pdf 文档
pdf
2601_9639067811 小时前
离线截图 OCR 识别:支持表格与 PDF 解析
pdf·ocr·软件需求
青 春 记 忆11 小时前
零基础入门python70:Docker Compose 编排完整后端
python·后端开发