当前位置:   article > 正文

使用python找到PDF文件的文本位置、字体大小、字体名称和字体颜色_python 判断pdf中字体是否为宋体

python 判断pdf中字体是否为宋体

看了https://cloud.tencent.com/developer/ask/sof/1162044,需要获得pdf文件的段落的字体大小。

正好在做这方面的工作,还是使用fitz,就可以获得字体的大小

具体思路是:现将pdf转换成html,在使用bs4解析html

具体代码如下:

pdf2html:将pdf转换成html,这一步在转换时,有时会丢失一些字体信息

pdf2list:调用pdf2html现将pdf转换成html,在使用BeautifulSoup对html进行解析。

import fitz
from bs4 import BeautifulSoup
from tqdm import tqdm
  1. def pdf2html(input_path):
  2. '''
  3. 将pdf转成html
  4. :param input_path:
  5. :return:
  6. '''
  7. doc = fitz.open(input_path)
  8. html_content = ''
  9. for page in tqdm(doc):
  10. html_content += page.get_text('html')
  11. # print('开始输出html文件')html_path
  12. # #html_content +="</body></html>"
  13. html_path = "d:/ann/input.html"
  14. with open(html_path, 'w', encoding='utf-8', newline='')as fp:
  15. fp.write(html_content)
  16. return html_content
  1. def decodestylestr(style_attrs, attr):
  2. '''
  3. 解析style
  4. :param style_attrs:
  5. :param attr:
  6. :return:
  7. '''
  8. attrvalue = ""
  9. styles = style_attrs.split(";")
  10. for sty in styles:
  11. k, v = sty.split(":")
  12. v = v.replace("pt", "")
  13. if k == attr:
  14. attrvalue = v
  15. return attrvalue
'
运行

  1. def pdf2list(input_path):
  2. '''
  3. 按照p节点提取pdf文本,按照 [文本,left,top,[(fontname、fongsize,fontcolor),]] (fontname、fongsize,fontcolor)一个或多个存储。
  4. :param input_path:
  5. :return:
  6. '''
  7. html_content = pdf2html(input_path) # pdf转html
  8. bs_obj = BeautifulSoup(html_content, "html.parser")
  9. #读取P节点
  10. ptag = bs_obj.findAll("p")
  11. contents = []
  12. # 取P节点下文本以及其对应的left值和font-family和font-size的值。
  13. for p in ptag:
  14. ptext = p.text
  15. ptextnospace = ptext.replace(" ", "")
  16. #如果当前节点text为空,则下一个
  17. if len(ptextnospace) == 0: # 当前文本为空字符串
  18. continue
  19. else:
  20. pass
  21. '''
  22. 读取P节点下的style属性
  23. '''
  24. postioninfo=('','',)
  25. if 'style' in p.attrs:
  26. attributes = p.attrs['style']
  27. leftvalue = decodestylestr(attributes, "left")
  28. topvalue = decodestylestr(attributes, "top")
  29. postioninfo=(leftvalue,topvalue)
  30. else:
  31. postioninfo=('','',)
  32. pass
  33. '''
  34. 获取P节点下的span节点,并读取取style属性,主要包括字体名称、字体大小、字体颜色,是否加粗pdf2html没有提取到。如果有也应该获取
  35. pspans = p.find_all("span",recursive=False ) recursive=False只获取当前节点下的子节点,不循环其孙子及以下节点
  36. '''
  37. pspans = p.find_all("span")
  38. pspansstyles = []
  39. for pspan in pspans:
  40. pspantext = pspan.text
  41. pspantext=pspantext.replace(" ","")
  42. if len(pspantext) > 0:#当前span节点不为空。
  43. if 'style' in pspan.attrs:
  44. attributes = pspan.attrs['style']
  45. fontfamilyvalue = decodestylestr(attributes, "font-family")
  46. fontsizevalue = decodestylestr(attributes, "font-size")
  47. fontcolorvalue = decodestylestr(attributes, "color")
  48. pspansstyle = (fontfamilyvalue, fontsizevalue,fontcolorvalue)
  49. if pspansstyle in pspansstyles:#如果字体样式已经存在,则删除,在增加,保持最后的是字体的样子,后续判断要用到字体大小
  50. pspansstyles.remove(pspansstyle)
  51. pspansstyles.append(pspansstyle)
  52. else:
  53. pass
  54. ptextattrs = [ptext, postioninfo, pspansstyles]
  55. contents.append(ptextattrs)
  56. return contents
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号