Forgot password?
 Create new account
View 99|Reply 2

PyMupdf提取字体

[Copy link]

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2023-6-28 02:19:31 |Read mode
打开Word,选择“黑体”,输入“口”,高级选项勾选“是否嵌入字体”。
导出PDF: $type 口.pdf (50.2 KB, Downloads: 27)
要从这个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()
Copy the Code

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

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2023-6-28 02:22:36
用文本编辑器打开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,应该都是字体文件的内容。

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2023-6-28 02:29:48
Page.get_fonts()查看字体信息
  1. input_path="口.pdf"
  2. import fitz
  3. doc = fitz.open(input_path)
  4. print(doc[0].get_fonts())
Copy the Code

[
  (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()查出的两个字体。

手机版Mobile version|Leisure Math Forum

2025-4-20 22:13 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list