Skip to content

Instantly share code, notes, and snippets.

@goderich
Created December 29, 2017 14:30
Show Gist options
  • Save goderich/ad42e1bc8f9b9c74ff8748f3fc534883 to your computer and use it in GitHub Desktop.
Save goderich/ad42e1bc8f9b9c74ff8748f3fc534883 to your computer and use it in GitHub Desktop.
import argparse
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument('file', help='A .doc or .docx file to open')
args = parser.parse_args()
class DocError(Exception):
'''Errors for non-Word type files'''
pass
class Doc:
'''Class for Word documents'''
def __init__(self, name):
self.name = name
if self.name.endswith('.doc'):
self.doctype = 'doc'
elif self.name.endswith('.docx'):
self.doctype = 'docx'
else:
raise DocError
def display_in_less(self):
if self.doctype == 'doc':
subprocess.run(['bash', '-c', 'antiword ' + self.name + ' | less'])
else:
subprocess.run(['bash', '-c', 'docx2txt ' + self.name + ' - | less'])
if __name__ == '__main__':
try:
f = Doc(args.file)
f.display_in_less()
except DocError:
print('Please give only .doc or .docx files as arguments.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment