怎么提取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")
相关推荐
一个java开发2 小时前
distributed.client.Client 用户可调用函数分析
大数据·python
eqwaak02 小时前
Matplotlib 动态显示详解:技术深度与创新思考
网络·python·网络协议·tcp/ip·语言模型·matplotlib
007php0073 小时前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
CodeCraft Studio3 小时前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
flashlight_hi3 小时前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode
java1234_小锋3 小时前
Scikit-learn Python机器学习 - 特征预处理 - 归一化 (Normalization):MinMaxScaler
python·机器学习·scikit-learn
星空的资源小屋3 小时前
网易UU远程,免费电脑远程控制软件
人工智能·python·pdf·电脑
IMER SIMPLE3 小时前
人工智能-python-深度学习-神经网络-MobileNet V1&V2
人工智能·python·深度学习
eleqi3 小时前
Python+DRVT 从外部调用 Revit:批量创建楼板
python·系统集成·revit·外部调用·drvt·自动化生产流水线
咖啡Beans4 小时前
Python工具DrissionPage推荐
后端·python