如何在PyMuPDF中提取字体名称而不使用子集?
我们正在使用 PyMuPDF 的 Page.get_fonts()
函数来从 PDF 中提取字体名称,但得到的字体名称中包含了一些子集。我们尝试在代码中使用 fitz.Tools.set_subset_fontnames()
设置,这个方法对 get_text()
返回的字体有效,但对 get_fonts()
就不管用了。
下面是我的示例代码:
import fitz
fitz.TOOLS.set_subset_fontnames(False)
file_path = "sample.pdf"
pdf_document = fitz.open(file_path)
for page in pdf_document:
extracted_fonts = page.get_page_fonts(full=True)
print(extracted_fonts)
这是我得到的输出:
[
(140, 'ttf', 'TrueType', 'XEAAAC+Arial Bold', 'F3', 'WinAnsiEncoding', 0),
(138, 'ttf', 'TrueType', 'XEAAAB+Times New Roman', 'F2', 'WinAnsiEncoding', 0),
(137, 'ttf', 'TrueType', 'XEAAAA+Arial', 'F1', 'WinAnsiEncoding', 0)
]
我想要的字体名称是没有子集的,比如说我希望得到 "Arial Bold"
,而不是 "XEAAAC+Arial Bold"
。
1 个回答
0
你可以用'+'这个符号把字体名称分开,然后选择最后一部分,这部分就是实际的字体名称,不包括前面的子集前缀:
import fitz
fitz.TOOLS.set_subset_fontnames(False)
file_path = "sample.pdf"
pdf_document = fitz.open(file_path)
for page in pdf_document:
extracted_fonts = page.get_fonts(full=True)
cleaned_fonts = [(font_id, font_format, font_type, font_name.split('+')[-1], font_flags, font_encoding, font_embedded) for font_id, font_format, font_type, font_name, font_flags, font_encoding, font_embedded in extracted_fonts]
print(cleaned_fonts)