mathType.py
1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#mathType.py
#A part of NonVisual Desktop Access (NVDA)
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
#Copyright (C) 2014 NV Access Limited
"""Utilities for working with MathType.
"""
import _winreg as winreg
import ctypes
import mathPres
def _getDllPath():
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Design Science\DSMT6\Directories", 0, winreg.KEY_WOW64_32KEY | winreg.KEY_QUERY_VALUE) as key:
return winreg.QueryValueEx(key, "AppSystemDir32")[0] + "\\MT6.dll"
lib = ctypes.windll[_getDllPath()]
def getMathMl(oleFormat, runForConversion=True):
"""Get MathML from a MathType OLEFormat object.
"""
if runForConversion:
oleFormat.DoVerb(2) # "RunForConversion"
mt = oleFormat.object._comobj
length = ctypes.c_long()
# 2 is get MathML
try:
if lib.MTGetLangStrFromEqn(mt, 2, None, ctypes.byref(length)) != 0:
raise RuntimeError
mathMl = (ctypes.c_char * length.value)()
if lib.MTGetLangStrFromEqn(mt, 2, ctypes.byref(mathMl), ctypes.byref(length)) != 0:
raise RuntimeError
finally:
# 1 is OLECLOSE_NOSAVE
lib.MTCloseOleObject(1, mt)
return mathPres.stripExtraneousXml(mathMl.value)