【问题标题】:How to count characters based on its font?如何根据字体计算字符数?
【发布时间】:2021-07-11 23:04:25
【问题描述】:

对于给定 PDF 文件中的每一页,都可以列出使用的字体:

$ pdffonts -f 10 -l 10 file.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
[none]                               Type 3            Custom           yes no  no      12  0
DIIDPF+ArialMT                       CID TrueType      Identity-H       yes yes yes     95  0
DIIEDH+Arial                         CID TrueType      Identity-H       yes yes no     101  0
DIIEBG+TimesNewRomanPSMT             CID TrueType      Identity-H       yes yes yes    106  0
DIIEDG+Arial                         CID TrueType      Identity-H       yes yes no     112  0
Arial                                TrueType          WinAnsi          yes no  no     121  0

我需要根据pdffonts 输出识别可能有问题的字体,并根据其字体计算字符数。我通过实现以下 sn-p 实现了它:

def count_fonts_ocurrencies_by_page(pdf_filepath):
    page_layout = next(extract_pages(pdf_filepath))

    fonts = []

    for element in page_layout:
        if isinstance(element, LTTextContainer):
            for text_line in element:
                for character in text_line:
                    if isinstance(character, LTChar):
                        fonts.append(character.fontname)

    return Counter(fonts)

我期待找到一种直接的方法来做同样的事情(或关闭,我只需要知道单个 PDF 页面上字体使用百分比之类的信息),而无需迭代每个字符(如果可能的话)或者可能不使用整个模块,比如 pdfminer,只用于一个功能和一个 PDF 页面。如果我可以使用 pdfminer 中的最少代码执行类似(重新)操作,那也会很有帮助,因为它是以模块化方式构建的。

【问题讨论】:

  • 文本渲染(编码、字形)和文本提取(ToUnicode、CMap)是有区别的吧?我同意我所问的不是一项微不足道的任务,但也许对我的问题的任何指导/澄清都会有所帮助和有价值。作为PDF新手,缺乏领域词汇也是一件事情

标签: python pdf pdfminer


【解决方案1】:

您可以尝试使用 pdffonts 的同一包中的 pdftohtml,然后使用 xpath 解析 html 文件并考虑样式

pdftohtml -f 1 -l 1 -c -s -i -fontfullname fonts.pdf

生成的文档

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<title>fonts-html.html</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <br/>
<style type="text/css">
<!--
    p {margin: 0; padding: 0;}  .ft10{font-size:16px;font-family:BAAAAA+NotoSans-CondensedExtraBold;color:#000000;}
    .ft11{font-size:16px;font-family:CAAAAA+DejaVuMathTeXGyre-Regular;color:#000000;}
    .ft12{font-size:13px;font-family:DAAAAA+Baekmuk-Headline;color:#000000;}
    .ft13{font-size:13px;font-family:EAAAAA+LMMono9-Regular;color:#000000;}
    .ft14{font-size:13px;font-family:FAAAAA+CantarellRegular;color:#000000;}
    .ft15{font-size:13px;font-family:GAAAAA+Courier;color:#000000;}
-->
</style>
</head>
<body bgcolor="#A0A0A0" vlink="blue" link="blue">
<div id="page1-div" style="position:relative;width:892px;height:1263px;">
<img width="892" height="1263" src="fonts001.png" alt="background image"/>
<p style="position:absolute;top:64px;left:86px;white-space:nowrap" class="ft10"><b>Font1</b></p>
<p style="position:absolute;top:91px;left:86px;white-space:nowrap" class="ft11">font3</p>
<p style="position:absolute;top:109px;left:86px;white-space:nowrap" class="ft12">font4</p>
<p style="position:absolute;top:124px;left:86px;white-space:nowrap" class="ft13">font5</p>
<p style="position:absolute;top:144px;left:86px;white-space:nowrap" class="ft14">font6</p>
<p style="position:absolute;top:163px;left:86px;white-space:nowrap" class="ft15">font7</p>
</div>
</body>
</html>

用python解析html并按字体计算字符(类属性)

from lxml import html                      
tree = html.parse(r'/home/luis/tmp/fonts-html.html')
eleList = tree.xpath("//p[@class='ft10']")
len(eleList[0].text_content())
# text length: 5 
eleList = tree.xpath("//p[@class[contains(.,'ft')]]")
eleList[0].get('class')
# class name: 'ft10'

【讨论】:

  • 非常感谢,它看起来很棒。到目前为止,我发现pdffontspdftohtml 之间唯一的不匹配是前者列出的[none] 命名字体。我不知道是否有任何字符使用[none](Type 3)字体,因为pdftohtml 仅列出“有效字体”(?)。 pdfminer.six 似乎可以命名和计算这些字符。对于我的一些案例,它发现Myriad pro 字体和pdffonts 将其命名为[none]。无论如何,我认为这个解决方案非常有帮助:)
  • 更多关于未命名的 type 3 字体:stackoverflow.com/questions/40233971/…
猜你喜欢
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多