-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoprint_vx.py
104 lines (86 loc) · 2.73 KB
/
autoprint_vx.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os, shutil, win32api, win32print, time
from datetime import datetime
from watchdog.observers import Observer
from watchdog.events import *
fileFold = '~/'
printedFold = '~/printed'
def print_doc(filename):
open(filename, "r")
win32api.ShellExecute(0, "print", filename,
'/d:"%s"' % win32print.GetDefaultPrinter(), ".", 0)
def print_pdf(filename):
os.system(r'start acrobat /P /h "%s"' % filename)
def findFile(fold):
files = os.listdir(fold)
fileList = []
for fileName in files:
file = [fileName, os.path.join(fold, fileName)]
if os.path.isdir(file[1]):
fileList = fileList + findFile(file[1])
else:
fileList.append(file)
return fileList
def cutSuffix(filename):
doc = ['.docx', '.doc']
if '.pdf' in filename:
i = filename[::-1].find('fdp.')
if i == 0:
return 1, filename
else:
return 1, filename[:-i]
for suffix in doc:
if suffix in filename:
j = filename[::-1].find(suffix[::-1])
if j == 0:
return 2, filename
else:
return 2, filename[:-j]
return 0
class handler(FileSystemEventHandler):
def on_created(self, event):
cuted = cutSuffix(event.src_path)
if cuted:
filetype = cuted[0]
newpath = cuted[1]
while True:
if os.path.exists(newpath):
break
time.sleep(1)
filename = os.path.split(newpath)[-1]
Time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
ctime = time.strftime("%Y%m%d-%H%M%S",
time.localtime(os.stat(newpath).st_ctime))
ctimeFold = os.path.join(printedFold, ctime)
if not os.path.exists(ctimeFold):
os.mkdir(ctimeFold)
filePath = os.path.join(ctimeFold, filename)
shutil.copy(newpath, filePath)
print('%s is printed %s' % (filename, Time))
# pushNotification(filename)
if filetype == 1:
print_pdf(filePath)
if filetype == 2:
print_doc(filePath)
def autoprint_vx():
observer = Observer()
observer.schedule(handler(), fileFold, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def main():
try:
print("Print from wechat start")
autoprint_vx()
except Exception:
print(Exception.__context__)
print('Restart in 60 seconds')
time.sleep(60)
main()
finally:
print('Sucssess')
if __name__ == '__main__':
main()