Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created May 10, 2012 13:32
Show Gist options
  • Save moluapple/2653007 to your computer and use it in GitHub Desktop.
Save moluapple/2653007 to your computer and use it in GitHub Desktop.
解析获取 PSD 文档所用字体列表
'''
此脚本为 python3 版。
'''
import re
def selectFile():
'''利用win32ui选择文件对话框
如果未安装此模块则弹窗输入路径'''
try:
import win32ui
o = win32ui.CreateFileDialog( 1, ".psd", "", 0, "Adobe Photoshop Files (*.psd)|*.psd|")
o.DoModal()
return o.GetPathName()
except Exception:
path = input('Enter File Path: ')
return re.sub('^"|"$', '', path)
def extractData(inputFile):
'''获取DocumentResources/FontSet起始位置
据此读取相应数据'''
regex_complex = re.compile(b'/FontSet(?:.(?!/Name))+ /Name \(([^(]+)\)')
regex_simple = re.compile(b'/Name \(([^(]+)\)')
regex_data = re.compile(b'/StreamTag /CoolTypeFont /Identifier << /Name \(([^(]+)\)')
doc_fonts = {}
with open(inputFile, 'rb') as fobj:
for line in fobj:
if b'/FontSet' in line:
if line == b'\t\t/FontSet [\n':
fobj.readline()
cleardata = fobj.readline().replace(b'\x00', b'').replace(b'\xfe\xff', b'')
font = regex_simple.findall(cleardata)[0]
doc_fonts[font] = 1
else:
cleardata = line.replace(b'\x00', b'').replace(b'\xfe\xff', b'')
# print(cleardata)
if b'/DocumentResources << /FontSet << /Resources [ << /Resource' in line:
info = regex_data.findall(cleardata)
else:
info = regex_complex.findall(cleardata)
for i in info:
if i != b'AdobeInvisFont':
doc_fonts[i] = 1
print(list(doc_fonts.keys()))
def main():
inputFile = selectFile()
extractData(inputFile)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment