Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created March 18, 2012 15:06
Show Gist options
  • Save moluapple/2074986 to your computer and use it in GitHub Desktop.
Save moluapple/2074986 to your computer and use it in GitHub Desktop.
EPS (Private/Print) Data Splitter (Illustrator EPS 文档(打印/私密)数据分离器)
'''
此脚本为 python3 版。
将 Illustrator EPS 文档分离为两个文件:
Private 文件为私密数据,用于 Illustrator 打开;
Print 文件为打印数据,用于置入其他程序,
注意不可用 Illustrator 打开,会导致失去响应。
'''
import re
def selectFile():
'''利用win32ui选择文件对话框
如果未安装此模块则弹窗输入路径'''
try:
import win32ui
o = win32ui.CreateFileDialog( 1, ".eps", "", 0, "Adobe illustrator EPS Files (*.eps)|*.eps|")
o.DoModal()
return o.GetPathName()
except Exception:
path = input('Enter File Path: ')
return re.sub('^"|"$', '', path)
def extractData(inputFile, dataType):
'''获取(Private/Print)Data起始及终结位置
据此读取相应EPS数据'''
start = 0
with open(inputFile, 'rb') as fobj:
if dataType == 'Private':
for line in fobj:
if b'AI9_PrivateDataBegin' in line:
start = fobj.tell()
if start and b'AI9_PrivateDataEnd' in line:
end = fobj.tell()
break
else:
for line in fobj:
if b'%!PS-Adobe-3.1 EPSF-3.0' in line:
start = fobj.tell() - 25
if b'%%EOF' in line:
end = fobj.tell()
break
fobj.seek(start, 0)
return fobj.read(end - start)
def writeEPS(inputFile, dataType):
'''将数据写入文件'''
outFile = re.sub(r'(?<=\\)([^\\]+)(?=\.eps)', lambda m: "%s_%s" % (m.group(1), dataType), inputFile, flags=re.IGNORECASE)
data = extractData(inputFile, dataType)
with open(outFile, 'wb') as fobj:
fobj.write(data)
def main():
inputFile = selectFile()
writeEPS(inputFile, 'Private')
writeEPS(inputFile, 'Print')
if __name__ == '__main__':
main()
@moluapple
Copy link
Author

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