forked from angelamchen/Jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jarvis.py
executable file
·55 lines (39 loc) · 1.29 KB
/
jarvis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/local/bin/python3
from PyPDF2 import PdfFileWriter, PdfFileReader
import os
import sys
# TODO: undoing a creation
def seperate_pdf(file):
print("Seperating pdf command triggered")
print("Initializing new directory...")
parent = os.getcwd()
directory = "pdf-output"
path = os.path.join(parent, directory)
os.mkdir(path)
print("Directory '{}' created".format(path))
filename = file[:-4]
print("Seperating {}...".format(file))
input = PdfFileReader(open(file, "rb"))
for i in range(input.numPages):
output = PdfFileWriter()
output.addPage(input.getPage(i))
with open("{}/{}-p{}.pdf".format(path, filename, i), "wb") as outputStream:
output.write(outputStream)
print("Done.")
def pdf(arguments):
action = arguments[0]
if action == "split":
if len(arguments) != 2:
raise Exception(
"Invalid arguments. Usage: jarvis pdf split [filename.pdf]")
seperate_pdf(arguments[1])
else:
raise Exception("Invalid command. Possible commands: jarvis pdf split")
if __name__ == "__main__":
num_args = len(sys.argv)
# Get command
command = sys.argv[1]
if command == "pdf":
pdf(sys.argv[2:])
else:
print("Invalid command. Please try again")