Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created March 17, 2012 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moluapple/2059569 to your computer and use it in GitHub Desktop.
Save moluapple/2059569 to your computer and use it in GitHub Desktop.
Extract PGF data form .ai document (AI 文档净化器)
'''
注:此脚本为 python3 版
'''
import re
def selectFile():
'''利用win32ui选择文件对话框
如果未安装此模块则弹窗输入路径'''
try:
import win32ui
o = win32ui.CreateFileDialog( 1, ".ai", "", 0, "Adobe illustrator Files (*.ai)|*.ai|")
o.DoModal()
return o.GetPathName()
except Exception:
path = input('Enter File Path: ')
return re.sub('^"|"$', '', path)
def extractData(inputFile):
'''简单解析获取PGF数据起始及终结位置
据此读取PGF数据'''
regex_bool = re.compile(b'/AIPrivateData\d+ (\d+) 0 R')
regex_data = re.compile(b'^([\s\S]+?)(?=(\d+) 0 obj\r?[^>]+/AIPrivateData)')
obj = 0
with open(inputFile, 'rb') as fobj:
for line in fobj:
if regex_bool.search(line):
# print(line)
# 测试表明CS2及以下版本保存之ai文件需排序
obj = sorted(regex_bool.findall(line), key = lambda x: int(x))
info = regex_data.findall(line)[0]
cut = len(info[0])
Private_Index = info[1]
start = fobj.tell() - len(line) + cut
if obj and (b'\r' + obj[-1] + b' 0 obj') in line:
end = fobj.tell() + int(re.compile(b'/Length (\d+)').findall(line)[0])
break
fobj.seek(start, 0)
return fobj.read(end - start + 18), Private_Index
def writePGF(outFile, data, index):
'''依次将文件头/PGF数据/文件尾写入文件'''
header = b'%PDF-1.5\n1 0 obj\n<</Pages 2 0 R>>\nendobj\n2 0 obj\n<</Kids[3 0 R]>>\nendobj\n3 0 obj\n<</Parent 2 0 R/PieceInfo<</Illustrator 4 0 R>>>>\nendobj\n4 0 obj\n<</Private 0 R>>\nendobj\n'
header = header.replace(b'/Private ', b'/Private ' + index)
trailer = b'\ntrailer\n<</Root 1 0 R>>\n%%EOF\n'
with open(outFile, 'wb') as fobj:
fobj.write(header)
fobj.write(data)
fobj.write(trailer)
def main():
inputFile = selectFile()
outFile = re.sub(r'(?<=\\)([^\\]+)(?=\.ai)', lambda m: "_%s" % m.group(1), inputFile, flags=re.IGNORECASE)
data, Private_Index = extractData(inputFile)
writePGF(outFile, data, Private_Index)
if __name__ == '__main__':
main()
@moluapple
Copy link
Author

@sxiii
Copy link

sxiii commented Dec 10, 2019

Hi @moluapple! Thanks for the module. I would ask:

  • Does the script work now? With the modern versions of AI files?
  • Can it be translated to english?

P.S. My current tryings:

python3 ./AI_PGF_Extract.py
Enter File Path: input.ai
Traceback (most recent call last):
  File "./AI_PGF_Extract.py", line 64, in <module>
    main()
  File "./AI_PGF_Extract.py", line 59, in main
    data, Private_Index = extractData(inputFile)
  File "./AI_PGF_Extract.py", line 34, in extractData
    info = regex_data.findall(line)[0]
IndexError: list index out of range

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment