textbase.py 732 B

123456789101112131415161718192021222324252627282930313233
  1. import math
  2. def __inc_dict_val(mp, key, val_inc:int):
  3. if mp.get(key):
  4. mp[key] = mp[key] + val_inc
  5. else:
  6. mp[key] = val_inc
  7. def get_text_block_base_info(block):
  8. """
  9. 获取这个文本块里的字体的颜色、字号、字体
  10. 按照正文字数最多的返回
  11. """
  12. counter = {}
  13. for line in block['lines']:
  14. for span in line['spans']:
  15. color = span['color']
  16. size = round(span['size'], 2)
  17. font = span['font']
  18. txt_len = len(span['text'])
  19. __inc_dict_val(counter, (color, size, font), txt_len)
  20. c, s, ft = max(counter, key=counter.get)
  21. return c, s, ft