interface fully working: conversion from docx is handdled by py library mammoth

master
Castro0o 7 years ago
parent 47e59d0eda
commit 9573dcd724

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import cgitb; cgitb.enable() import cgitb; cgitb.enable()
import cgi, os, sys, subprocess, shlex import cgi, os, sys, subprocess, shlex, mammoth
from tempfile import NamedTemporaryFile as NTF from tempfile import NamedTemporaryFile as NTF
from settings import PANDOC from settings import PANDOC
@ -83,36 +83,21 @@ if method == "POST":
if download: if download:
print "Content-Disposition: attachment;filename=\"hype_conversion.{0}\"".format(to_type['ext']) print "Content-Disposition: attachment;filename=\"hype_conversion.{0}\"".format(to_type['ext'])
print print
# p = subprocess.check_output('pandoc --from {0} --to {1} "{2}"'.format(ffrom, to, tmp.name), shell=True, stderr=subprocess.STDOUT)
# read content of tmp docx
cmd_head = "head {}".format(tmp.name)
cmd_head_list = shlex.split( cmd_head )
p_head = subprocess.Popen(cmd_head_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# convert docx to html: error. No idea why docx_file = open(tmp.name, "rb")
cmd_unoconv = "unoconv --stdout -f html {}".format(tmp.name) result = mammoth.convert_to_markdown(docx_file)
cmd_unoconv_list = shlex.split( cmd_unoconv ) out = (result.value).encode('utf-8')
p_unoconv = subprocess.Popen(cmd_unoconv_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err = result.messages
# convert docx to html: depends on most recente versions of pandoc print out#, err
# cmd = "{} --from {} --to {} {}".format(PANDOC, from_format, to_format, tmp.name)
# cmd_list = shlex.split( cmd )
# p = subprocess.Popen(cmd_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# print p
# p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p_head.communicate() # p_unoconv.communicate()
#print tmp.name, from_format
print out, err
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError, e:
print "Content-type: text/html;charset=utf-8" print "Content-type: text/html;charset=utf-8"
print print
print u"<span style='font-family:monospace'>An error occurred, pandoc said: {0}</span>".format(e.output).format("utf-8") print u"<span style='font-family:monospace'>An error occurred, pandoc said: {0}</span>".format(e.output).format("utf-8")
# tmp.unlink(tmp.name) # remove file tmp.unlink(tmp.name) # remove file
################################### ###################################
# FORM/TEXT INPUT # FORM/TEXT INPUT
@ -136,7 +121,7 @@ if method == "POST":
out, err = p.communicate(text) out, err = p.communicate(text)
print cmd+' '+ out print out
except subprocess.CalledProcessError, e: except subprocess.CalledProcessError, e:
print "Content-type: text/html;charset=utf-8" print "Content-type: text/html;charset=utf-8"

Loading…
Cancel
Save