Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import os
- import re
- import sys
- import argparse
- import subprocess
- def main():
- parser = argparse.ArgumentParser(
- description="Convert a pdf file to plain text.")
- parser.add_argument("executable",
- help="File path of 'pdftotext' executable.")
- parser.add_argument("source",
- help='File path of document to be converted.')
- parser.add_argument("--print", action='store_true',
- help='Print file contents to STDOUT.')
- args = parser.parse_args()
- convert_document(executable, source)
- # build path of target file
- (target_dir, source_file) = os.path.split(rsource)
- target_file = re.sub(r'pdf$', 'txt', source_file)
- # check if target file exists
- if os.path.isfile(target_file):
- print("Successfully wrote file '{}'".format(target_file))
- else:
- print("Error occured during conversion. Exiting...")
- sys.exit(1)
- # optionally print contents
- if args.print:
- print_contents(os.path.join(target_dir, target_file))
- def convert_document(exe, doc):
- subprocess.call([exe, doc])
- def print_contents(fname):
- with open(fname, 'r') as fin:
- print(fin.read(), end="")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement