Created
March 17, 2012 14:06
-
-
Save moluapple/2059569 to your computer and use it in GitHub Desktop.
Extract PGF data form .ai document (AI 文档净化器)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
注:此脚本为 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @moluapple! Thanks for the module. I would ask:
P.S. My current tryings:
Thanks a lot.