Last active
April 1, 2019 12:14
-
-
Save nirlanka/f8550300a2e7149e9ae01c4b7582229f to your computer and use it in GitHub Desktop.
Drag .docx/.md file into this file to convert it to the other type!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Requires Pandoc. | |
### Install this using `choco install pandoc` | |
### | |
import sys | |
import subprocess | |
def log(txt): | |
outfile = open('convert.log.txt', 'a') | |
print(txt) | |
outfile.write(txt) | |
outfile.write('\n') | |
outfile.close() | |
_args = sys.argv | |
args = ' '.join(_args) | |
log(args) | |
try: | |
origfile = args.replace('\\', '/').split('.py ')[-1] | |
log(origfile) | |
ext = origfile.split('.')[-1] | |
_filename = origfile.split('.'+ext) | |
filename = _filename[0] | |
log(' '.join([filename, ext])) | |
if ext == 'docx': | |
log(ext) | |
cmd = 'pandoc --extract-media=. "{}.docx" -o "{}.md"'.format(filename, filename) | |
log(cmd) | |
console_output = subprocess.getoutput(cmd) | |
log(console_output) | |
elif ext == 'md': | |
log(ext) | |
cmd = 'pandoc -o "{}.docx" -f markdown -t docx "{}.md"'.format(filename, filename) | |
log(cmd) | |
console_output = subprocess.getoutput(cmd) | |
log(console_output) | |
except: | |
log('\n'.join(sys.exc_info())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment