找回密码
 快速注册
搜索
查看: 32|回复: 2

PyMupdf提取字体

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2023-6-28 02:19 |阅读模式
打开Word,选择“黑体”,输入“口”,高级选项勾选“是否嵌入字体”。
导出PDF: $type 口.pdf (50.2 KB, 下载次数: 0)
要从这个PDF中提取字体,运行下面的Python脚本
  1. input_path="口.pdf"
  2. import fitz
  3. doc = fitz.open(input_path)
  4. name, ext, _, content=doc.extract_font(doc[0].get_fonts()[0][0])
  5. ofile = open(name+"."+ext, "wb")
  6. ofile.write(content)
  7. ofile.close()
复制代码

提取出了字体 $type BCDEEE SimHei.ttf (124.53 KB, 下载次数: 0)
这应该是SimHei的子集,因为它远小于C:\Windows\Fonts\simhei.ttf的文件尺寸9.30MB

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-6-28 02:22
用文本编辑器打开PDF第30-21行:
9 0 obj
<</Type/FontDescriptor/FontName/BCDEEE+SimHei/Flags 32/ItalicAngle 0/Ascent 859/Descent -141/CapHeight 859/AvgWidth 500/MaxWidth 1008/FontWeight 400/XHeight 250/Leading 141/StemV 50/FontBBox[ -12 -141 996 859] /FontFile2 26 0 R>>
endobj

从上面的 /FontName 看出,字体名称 BCDEEE+SimHei
与上面提取出的文件名相符。

从上面的 /FontFile2 看出,字体文件的 xref 为 26
在第65行找到
26 0 obj
<</Filter/FlateDecode/Length 24427/Length1 127520>>
stream

后面是二进制数据,直到第252行endstream,应该都是字体文件的内容。

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-6-28 02:29
Page.get_fonts()查看字体信息
  1. input_path="口.pdf"
  2. import fitz
  3. doc = fitz.open(input_path)
  4. print(doc[0].get_fonts())
复制代码

[
  (5, 'ttf', 'Type0', 'BCDEEE+SimHei', 'F1', 'Identity-H'),
  (12, 'ttf', 'TrueType', 'BCDFEE+Calibri', 'F2', 'WinAnsiEncoding')
]

在18-20行找到xref为5的object
5 0 obj
<</Type/Font/Subtype/Type0/BaseFont/BCDEEE+SimHei/Encoding/Identity-H/DescendantFonts 6 0 R/ToUnicode 25 0 R>>
endobj

在39-40行找到xref为12的object
12 0 obj
<</Type/Font/Subtype/TrueType/Name/F2/BaseFont/BCDFEE+Calibri/Encoding/WinAnsiEncoding/FontDescriptor 13 0 R/FirstChar 32/LastChar 32/Widths 28 0 R>>
endobj

就是上面的Page.get_fonts()查出的两个字体。

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:18

Powered by Discuz!

× 快速回复 返回顶部 返回列表