-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
55 lines (44 loc) · 1.3 KB
/
test.py
File metadata and controls
55 lines (44 loc) · 1.3 KB
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
import glob, os
def readfile(filedir):
os.chdir(filedir)
return glob.glob("*.rtf")
def makedir(ndir):
try:
os.makedirs(ndir)
except OSError:
pass
def combinertf(filedir, filename,pagedelimit = True):
"""
filedir: directory of the file containing all rtfs needed to be combined
filename: the name of final combined rtf document.
pagedelimit: determine if rtf would start in a new page or new line.
"""
if '/' in filename:
makedir('/'.join(filename.split('/')[:-1]))
else:
makedir('output')
filename = 'output/' + filename
filenames = readfile(filedir)
test = filename
try:
filenames.remove(test)
except ValueError:
pass
out_file = open(test,'wb')
out_file.write("{")
for fname in filenames:
if test in fname: continue
with open(fname, 'rb') as f1:
mylist = list(l1 for l1 in f1)
mylist[0] = mylist[0].strip()[1:]
mylist[-1] = mylist[-1].strip()[:-1]
for i in mylist:
out_file.write(i)
if pagedelimit & (fname != filenames[-1]):
out_file.write("\par \page")
out_file.write("} ")
out_file.close()
def main():
combinertf('.','output.rtf',True)
if __name__ == '__main__':
main()