-
Notifications
You must be signed in to change notification settings - Fork 2
/
docx_to_txt.rb
executable file
·197 lines (146 loc) · 4.03 KB
/
docx_to_txt.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby
# encoding: utf-8
##########################################################
###
## File: docx_to_txt.rb
## Desc: Convert docx files to txt files
## By: Dewayne VanHoozer ([email protected])
#
require 'debug_me' # A tool to print the labeled value of variables.
include DebugMe
require 'pathname' # STDLIB
require 'pathname_helpers'
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 = {
verbose: false,
debug: false,
docx_pathnames: []
}
def verbose?
$options[:verbose]
end
def debug?
$options[:debug]
end
usage = <<EOS
Convert any docx file into a simple txt file.
Usage: #{my_name} [options] files_or_directories
Where:
options Do This
-h or --help Display this message
-v or --verbose Display progress
files_or_directories The locations of docx files
EOS
# Check command line for Problems with Parameters
$errors = []
$warnings = []
# Get the next ARGV parameter after param_index
def get_next_parameter(param_index)
unless Fixnum == param_index.class
param_index = ARGV.find_index(param_index)
end
next_parameter = nil
if param_index+1 >= ARGV.size
$errors << "#{ARGV[param_index]} specified without parameter"
else
next_parameter = ARGV[param_index+1]
ARGV[param_index+1] = nil
end
ARGV[param_index] = nil
return next_parameter
end # def get_next_parameter(param_index)
# Get $options[:out_filename]
def get_out_filename(param_index)
filename_str = get_next_parameter(param_index)
$options[:out_filename] = Pathname.new( filename_str ) unless filename_str.nil?
end # def get_out_filename(param_index)
# Display global warnings and errors arrays and exit if necessary
def abort_if_errors
unless $warnings.empty?
STDERR.puts
STDERR.puts "The following warnings were generated:"
STDERR.puts
$warnings.each do |w|
STDERR.puts "\tWarning: #{w}"
end
STDERR.print "\nAbort program? (y/N) "
answer = (gets).chomp.strip.downcase
$errors << "Aborted by user" if answer.size>0 && 'y' == answer[0]
$warnings = []
end
unless $errors.empty?
STDERR.puts
STDERR.puts "Correct the following errors and try again:"
STDERR.puts
$errors.each do |e|
STDERR.puts "\t#{e}"
end
STDERR.puts
exit(-1)
end
end # def abort_if_errors
# Display the usage info
if ARGV.empty? ||
ARGV.include?('-h') ||
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[ -d --debug ].each do |param|
if ARGV.include? param
$options[:debug] = true
ARGV[ ARGV.index(param) ] = nil
end
end
ARGV.compact!
def add_children_to_list(a_directory)
return unless a_directory.exist?
return unless a_directory.directory?
a_directory.children.each do |c|
add_children_to_list(c) if c.directory?
next unless '.docx' == c.extname.downcase
$options[:docx_pathnames] << c.realpath
end
end
ARGV.each do |arg|
file_path = Pathname.new arg
next unless file_path.exist?
add_children_to_list(file_path) if file_path.directory?
next unless '.docx' == file_path.extname.downcase
$options[:docx_pathnames] << file_path.realpath
end
if $options[:docx_pathnames].empty?
$errors << 'No docx files were found'
end
abort_if_errors
######################################################
# Local methods
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
pp $options if verbose? || debug?
$options[:docx_pathnames].each do |docx_path|
puts docx_path.basename
txt_path = Pathname.new( docx_path.to_s.gsub('.docx', '.txt') )
docx = open_docx(docx_path)
txt_file = File.open(txt_path, 'w')
docx.paragraphs.each do |a_para|
txt_file.puts a_para.text
end
txt_file.close
end