Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created March 22, 2012 08:19
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/2157102 to your computer and use it in GitHub Desktop.
Save moluapple/2157102 to your computer and use it in GitHub Desktop.
[Indesign] Word DLL 简繁转换 python 脚本打包测试
'''
抽出WORD中繁简转换的DLL,直接调用.
需要的文件: MSTR2TSC.DLL MSTR2TSC.LEX MSO.DLL
原文见:http://hyry.dip.jp:8000/code.py?id=105
'''
import ctypes
import win32com.client
def translate(s, gb_big):
d = ctypes.windll.LoadLibrary('MSTR2TSC.DLL')
d.TCSCInitialize()
ptr, num = ctypes.c_wchar_p(0), ctypes.c_int(0)
d.TCSCConvertText(s, len(s), ctypes.byref(ptr), ctypes.byref(num), gb_big, False, True)
# 1 or 0 简 -> 繁, False or True 转换常用词、使用港台异体字
out = ptr.value
d.TCSCFreeConvertedText(ptr)
d.TCSCUninitialize()
return out
def transId():
app = win32com.client.Dispatch('Indesign.Application.CS5')
text = app.ActiveDocument.Selection
gb_big = app.DoScript('confirm("是(Enter):繁>简\\n否(Esc):简>繁", 0, "简繁转换")', 1246973031)
try:
text.Contents = translate(text.Contents, gb_big)
except AttributeError:
text[0].Contents = translate(text[0].Contents, gb_big)
if __name__ == '__main__':
transId()
@moluapple
Copy link
Author

采用 cx_Freeze 打包。打包好的程序在此下载
运行方法:解压至 ID CS5 脚本文件夹,选中文本或文本框后,脚本面板双击.exe文件运行。

打包所用 setup.py 如下:

from cx_Freeze import setup, Executable

setup(
        name = "word_py3",
        version = "0.1",
        description = "word_py3简繁转换",
        executables = [Executable("word_py3.pyw")])

命令行输入 python setup.py build 运行。

@BrandonStudio
Copy link

链接已失效,另外这个方法现在似乎用不了了,dll导入时会提示找不到模块

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