Skip to content

Instantly share code, notes, and snippets.

@zhaofeng-shu33
Last active May 15, 2020 10:34
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 zhaofeng-shu33/34b300dc41fbd0f13b209efd4c6526ef to your computer and use it in GitHub Desktop.
Save zhaofeng-shu33/34b300dc41fbd0f13b209efd4c6526ef to your computer and use it in GitHub Desktop.
convert scientific workplace generated tex files to normal tex files
#!/usr/bin/python
#coding=utf-8
import re
import os
def toUnicode(num):
try:
return eval("u"+"'\u{0}'".format(num))
except Exception as e:
import pdb
pdb.set_trace()
# file_name=raw_input(u"input file to be decoded:")
def convert(file_name, dry_run):
try:
f=open(file_name,'r')
except IOError,reason:
print reason
else:
st=f.read()
f.close()
#st=re.sub('\\\\begin\{document\}','\\\\begin{document}\n\\\',st,1)
#st=re.sub('\\\\usepackage\{amsmath\}','\\\\usepackage{amsmath}\n\\\',st,1)
#st=re.sub('\\\\end\{document\}','\\\\n\\\\end{document}',st,1)
a=re.findall('U\{(\w*)\}',st)
b=[]
for item in a:
b.append(toUnicode(item))
st=st.decode('utf-8')
for item in b:
st=re.sub('\\\\U\{\w*\}',item,st,1)
st=st.encode('utf-8')
# file_name=file_name.replace('.tex','_new.tex')
if dry_run == False:
f=open(file_name,'w')
f.write(st)
f.close()
def convert_whole(dry_run):
for i in os.listdir('./'):
if i.find('.tex') > 0:
convert(i, dry_run)
if __name__ == '__main__':
convert_whole(True)
convert_whole(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment