Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import os
  4. import re
  5. import sys
  6. import argparse
  7. import subprocess
  8.  
  9.  
  10. def main():
  11. parser = argparse.ArgumentParser(
  12. description="Convert a pdf file to plain text.")
  13. parser.add_argument("executable",
  14. help="File path of 'pdftotext' executable.")
  15. parser.add_argument("source",
  16. help='File path of document to be converted.')
  17. parser.add_argument("--print", action='store_true',
  18. help='Print file contents to STDOUT.')
  19. args = parser.parse_args()
  20.  
  21. convert_document(executable, source)
  22.  
  23. # build path of target file
  24. (target_dir, source_file) = os.path.split(rsource)
  25. target_file = re.sub(r'pdf$', 'txt', source_file)
  26.  
  27. # check if target file exists
  28. if os.path.isfile(target_file):
  29. print("Successfully wrote file '{}'".format(target_file))
  30. else:
  31. print("Error occured during conversion. Exiting...")
  32. sys.exit(1)
  33.  
  34. # optionally print contents
  35. if args.print:
  36. print_contents(os.path.join(target_dir, target_file))
  37.  
  38.  
  39. def convert_document(exe, doc):
  40. subprocess.call([exe, doc])
  41.  
  42.  
  43. def print_contents(fname):
  44. with open(fname, 'r') as fin:
  45. print(fin.read(), end="")
  46.  
  47.  
  48. if __name__ == "__main__":
  49. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement