-
Notifications
You must be signed in to change notification settings - Fork 2
/
docx_rm_para_with_styles.rb
executable file
·182 lines (147 loc) · 4.07 KB
/
docx_rm_para_with_styles.rb
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env ruby
##########################################################
###
## File: docx_rm_para_with_styles.rb
## Desc: Remove all paragraphs with the given styles from
## all docx files in a given directory.
## By: Dewayne VanHoozer ([email protected])
#
require 'amazing_print' # Pretty print Ruby objects with proper indentation and colors
require 'pp'
require 'pathname' # STDLIB
require 'docx' # a ruby library/gem for interacting with .docx files
require('docx_helpers')
include DocxHelpers
me = Pathname.new(__FILE__).realpath
my_dir = me.parent
my_name = me.basename.to_s
$options = {
sub_dirname: "temp_styles_removed",
verbose: false,
styles: [],
paths: []
}
usage = <<EOS
Remove all paragraphs havin the given styles from
all paragraphs in a given directory
Usage: #{my_name} [options] [-s StyleName]* docx_filepaths|directories
Where:
options Do This
-h or --help Display this message
-v or --verbose display the paragraphs being removed
-s or --styles specifies a comma seperated list of
paragraph style name
docx_filepaths | space seperated list of a combination
directories file nad directory paths. If a directory
is specified, all of the docx within the
directory will be processed. There is
no recursion into sub-directories.
NOTE:
The original files are not modified. The files with the
styles removed are stored in a sub-directory within their
parant directory named: #{$options[:sub_dirname]}
EOS
def verbose?
$options[:verbose]
end
def get_style_names_after_param(param)
i = ARGV.index param
ARGV[i] = nil
i += 1
unless ARGV[i].nil?
$options[:styles] << ARGV[i].split(',')
ARGV[i] = nil
else
errors << "Style names not specified after #{param}."
end
end
errors = []
if ARGV.empty? or
ARGV.include? '-h' or
ARGV.include?'--help'
puts usage
exit
end
%w[ -v --verbose ].each do |param|
if ARGV.include? param
$options[:verbose] = true
ARGV[ ARGV.index(param) ] = nil
end
end
%w[ -s --styles ].each do |param|
if ARGV.include? param
get_style_names_after_param param
end
end
ARGV.compact!
if ARGV.empty?
errors << "No files or directories were given."
else
ARGV.each do |param|
path = Pathname.new(param)
if path.exist?
$options[:paths] << path.realpath
if path.file? and '.docx' != path.extname.downcase
errors << "Not *.docx file: #{path.basename}"
end
else
errors << "Unknown file/directory: #{param}"
end
end
end
unless errors.empty?
puts
puts "Correct the following errors and try again:"
puts
errors.each do |e|
puts "\t#{e}"
end
puts
exit(1)
end
$options[:styles].flatten!
$options[:styles].uniq!
$options[:paths].flatten!
$options[:paths].uniq!
######################################################
# Local methods
def out_filepath(in_filepath)
temp_dir = in_filepath.parent + $options[:sub_dirname]
temp_dir.mkdir unless temp_dir.exist?
temp_dir + in_filepath.basename
end
def remove_paragraphs_from_docx_file (docx_filepath)
puts "Processing #{docx_filepath.basename} ..." if verbose?
docx = Docx::Document.open(docx_filepath)
changed = false
docx.paragraphs.each do |para|
p_style = get_paragraph_style_name(para)
if $options[:styles].include? p_style
print "Delete " if verbose?
changed = true
delete_paragraph(para)
else
print " " if verbose?
end
puts p_style if verbose?
end
if changed
docx.save(out_filepath(docx_filepath))
end
end
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
$options[:paths].each do |path|
if path.file? and '.docx' == path.extname.downcase
remove_paragraphs_from_docx_file(path)
else
path.children.each do |c|
remove_paragraphs_from_docx_file(c) if c.file? and '.docx' == path.extname.downcase
end
end
end