Created
December 29, 2017 14:30
-
-
Save goderich/ad42e1bc8f9b9c74ff8748f3fc534883 to your computer and use it in GitHub Desktop.
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
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