2

我正在尝试使用 PyMUPDF 1.18.14 从 PDF 中提取粗体文本元素。我希望这能像我从flags=4针对粗体字体的文档中所理解的那样工作。

page = doc[1]
text = page.get_text(flags=4)
print(text)

但它会打印出页面上的所有文本,而不仅仅是粗体文本。

当使用TextPage.extractDICT() (or Page.get_text(“dict”))这样的: -

page.get_text("dict", flags=11)["blocks"]

该标志有效,但我无法理解它在做什么。也许在图像和文本块之间切换。

跨度

因此,您似乎必须到达 才能span访问标志。

<page>
    <text block>
        <line>
            <span>
                <char>
    <image block>
        <img>

在此处输入图像描述

所以你可以做这样的事情,我flags=20在 span 标签上使用来获得粗体字体。

page = doc[1]
blocks = page.get_text("dict", flags=11)["blocks"]
for b in blocks:  # iterate through the text blocks
    for l in b["lines"]:  # iterate through the text lines
        for s in l["spans"]:  # iterate through the text spans
            if s["flags"] == 20:  # 20 targets bold
                print(s)

但这似乎很遥远。

所以我的问题是这是找到粗体元素的最佳方式,还是我遗漏了什么?

能够使用搜索粗体元素会很棒page.search_for()

4

0 回答 0