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


    思路

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

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

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

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

    1. import pdfplumber
    2. import glob,os
    3. WordDict = dict()
    4. def isWord(word):
    5. retVal = True
    6. if len(word) < 5 or word.isidentifier() == False or word.isascii() == False:
    7. retVal = False
    8. else:
    9. for c in word:
    10. if c in ['0','1', '2', '3', '4', '5', '6', '7', '8', '9', '_']:
    11. retVal = False
    12. return retVal
    13. #DIR=r"E:\GetEnglishDictionary"
    14. DIR = os.getcwd() + "\\workspace\\"
    15. temp=os.listdir(DIR)
    16. Dirlist=[]
    17. for i in temp:
    18. if (i.find(".pdf"))!= -1:
    19. Dirlist.append(i)
    20. try:
    21. # out
    22. for dir in Dirlist:
    23. print ("Analyse {} file".format(dir))
    24. #file=glob.glob(os.path.join(DIR+"\\"+dir, "*.*"))
    25. pdffile = DIR + "\\" + dir
    26. wordDictFile = pdffile.replace(".pdf", "_dict") + ".txt"
    27. dictFile = open(wordDictFile, 'w', encoding="utf-8")
    28. with pdfplumber.open(pdffile) as pdf:
    29. #for j in range(1, 2):
    30. pageNum = len(pdf.pages)
    31. progress = 0
    32. now = 0
    33. pageIndex = 0
    34. for page in pdf.pages:
    35. pageIndex = pageIndex + 1
    36. progress = int(pageIndex * 100/ pageNum)
    37. if progress >= now + 1:
    38. print(pdffile + " : " + str(progress) + " %")
    39. now = progress
    40. # 读取PDF文档第i+1页
    41. #page = pdf.pages[j]
    42. # page.extract_text()函数即读取文本内容
    43. txt = page.extract_text()
    44. txt = txt.replace(',', ' ')
    45. txt = txt.replace('\n', ' ')
    46. #words = ''.join(txt.split('\n')[:-1])
    47. #vols = str(words).split(' ')
    48. vols = str(txt).split(' ')
    49. for vol in vols:
    50. if isWord(vol) == True:
    51. #print(vol)
    52. tst = WordDict.get(vol.capitalize())
    53. if tst == None:
    54. WordDict[vol.capitalize()] = vol
    55. dictFile.write( vol + "\n")
    56. #str(pageIndex) + " " +
    57. ##
    58. dictFile.close()
    59. print("共 " + str(pageNum) + " 页,提取单词:" + str(len(WordDict)) + " 个")
    60. except Exception as e :
    61. print(repr(e))
    62. finally:
    63. print("finish write")

  • 相关阅读:
    mysql的mysql_store_result函数调用问题(C的API)
    橘子学linux03之Linux文件管理(上)
    查找mac硬盘序列号的方法
    前端工程师面试题库
    购买服务器,并安装宝塔
    springboot 2.0 集成 shiro 和 thymeleaf-extras-shiro 2.0 使用过程遇到的问题
    如何查询文件夹中文本文件的内容 (LINQ) (C#)
    【实践篇】一次Paas化热部署实践分享 | 京东云技术团队
    Docker 使用 IDEA 内置插件构建上传镜像 与 SSH、FTP 功能使用
    【C++】特殊类的设计
  • 原文地址:https://blog.csdn.net/xianghuaizi/article/details/139279426