-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir2dox.py
executable file
·117 lines (80 loc) · 3.23 KB
/
dir2dox.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
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
"""
dir2docx - convert files from directory and sub direcetories recursivly to a docx
"""
import os
import argparse
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt
def log(txt):
""" Print txt to stdout if verbose is TRUE """
if ARGS.verbose:
print(txt)
def generate_docx(path, outfile, lvl=1):
""" Creates document and parses directory recursivly to generate docx file """
document = Document()
document = create_code_style(document)
add_dir_to_dox(path, document, lvl)
document.save(outfile)
def create_code_style(document):
obj_styles = document.styles
codestyle = obj_styles.add_style('Code_style', WD_STYLE_TYPE.PARAGRAPH)
codestyle.base_style = obj_styles['Normal']
codestyle.font.size = Pt(9)
codestyle.font.name = 'Consolas'
return document
def all_files_in_dir(dir_path):
""" Returns non-hidden files in dir_path, if ARGS.all then all files are returned """
dir_entries = sorted(os.listdir(dir_path))
if not ARGS.all:
dir_entries = [entry for entry in dir_entries if not entry.startswith('.')]
return dir_entries
def add_dir_to_dox(dir_path, document, lvl):
""" Adds all files in dir and enters directories recursively to do the same """
log("Entering %s" % dir_path)
dir_entries = all_files_in_dir(dir_path)
for entry in dir_entries:
path = os.path.join(dir_path, entry)
if os.path.isdir(path):
(_, title) = os.path.split(path)
document.add_heading(title, lvl)
add_dir_to_dox(path, document, lvl+1)
else:
add_file_to_docx(path, document, lvl)
def add_file_to_docx(path, document, lvl):
""" Adds file to document if it is not in ignore list """
extension = path.split(".")[-1]
if extension in ARGS.ignore:
log("Skipping %s" % path)
return
log("Adding %s" % path)
title = os.path.basename(path)
document.add_heading(title, lvl)
if os.path.splitext(path)[-1] in ['.txt','.md','.markdown']:
style = 'Normal'
else:
style = 'Code_style'
log("> Applying {style} style for {ext}".format(ext=os.path.splitext(path), style=style))
with open(path) as f:
p = document.add_paragraph(f.read(), style=style)
def parse_args():
""" Setup arguments"""
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--all", action="store_true",
help="Parse all files and don't skip over hidden files & folders")
parser.add_argument("-p", "--path", type=str,
default=".",
help="Top level directory path to scan")
parser.add_argument("-o", "--outfile", type=str,
default="output.docx",
help="Name of output file")
parser.add_argument("-i", "--ignore", nargs='+', type=str,
default=["exe"],
help="File extensions to ignore and not add to the docx")
parser.add_argument("-v", "--verbose", action="store_true",
help="verbose")
return parser.parse_args()
if __name__ == '__main__':
ARGS = parse_args()
generate_docx(ARGS.path, ARGS.outfile)