-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc2txt.py
executable file
·283 lines (265 loc) · 8.1 KB
/
doc2txt.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#! /usr/bin/env python3
"""Document transformation wrapper"""
import logging
import os
import shutil
import sys
import textwrap
from pathlib import Path # https://docs.python.org/3/library/pathlib.html
from subprocess import PIPE, Popen, call
from urllib.request import urlopen
HOME = os.path.expanduser("~")
DST_FILE = HOME + "/tmp/.pw/dt-result.txt"
PANDOC_BIN = shutil.which("pandoc")
VISUAL = os.environ["VISUAL"]
if not all([HOME, VISUAL, PANDOC_BIN]):
raise FileNotFoundError("Your environment is not configured correctly")
log_level = 100 # default
critical = logging.critical
info = logging.info
dbg = logging.debug
def rotate_files(filename, max_rot=5):
"""Create at most {max_rot} rotating files."""
bare, ext = os.path.splitext(filename)
for counter in reversed(range(2, max_rot + 1)):
old_filename = f"{bare}{counter-1}{ext}"
new_filename = f"{bare}{counter}{ext}"
if os.path.exists(old_filename):
os.rename(old_filename, new_filename)
if os.path.exists(filename):
os.rename(filename, f"{bare}1{ext}")
if __name__ == "__main__":
import argparse # http://docs.python.org/dev/library/argparse.html
arg_parser = argparse.ArgumentParser(
description=(
"Document transformation wrapper which (by default) converts HTML to text"
)
)
arg_parser.add_argument("filename", nargs=1, metavar="FILE_NAME")
arg_parser.add_argument(
"-m",
"--markdown",
action="store_true",
default=False,
help="file2mdn via pandoc (quite busy with links)",
)
arg_parser.add_argument(
"-p",
"--plain",
action="store_true",
default=False,
help="file2txt via pandoc",
)
arg_parser.add_argument(
"-y",
"--lynx",
action="store_true",
default=False,
help="html2txt via lynx (nice formatting)",
)
arg_parser.add_argument(
"-i",
"--links",
action="store_true",
default=False,
help="html2txt via links",
)
arg_parser.add_argument(
"-3",
"--w3m",
action="store_true",
default=False,
help="html2txt via w3m",
)
arg_parser.add_argument(
"-a",
"--antiword",
action="store_true",
default=False,
help="doc2txt via antiword",
)
# arg_parser.add_argument( # deprecated, not on homebrew
# "-c", "--catdoc",
# action="store_true", default=False,
# help="doc2txt via catdoc")
arg_parser.add_argument(
"-d",
"--docx2txt",
action="store_true",
default=False,
help="docx2txt via docx2txt",
)
arg_parser.add_argument(
"-t",
"--pdftotext",
action="store_true",
default=False,
help="pdf2txt via pdftotext",
)
arg_parser.add_argument(
"-w", "--wrap", action="store_true", default=False, help="wrap text"
)
arg_parser.add_argument(
"-q",
"--quote",
action="store_true",
default=False,
help="prepend '>' quote marks to lines",
)
arg_parser.add_argument(
"-L",
"--log-to-file",
action="store_true",
default=False,
help="log to file PROGRAM.log",
)
arg_parser.add_argument(
"-V",
"--verbose",
action="count",
default=0,
help="Increase verbosity (specify multiple times for more)",
)
arg_parser.add_argument("--version", action="version", version="TBD")
args = arg_parser.parse_args()
if args.verbose == 1:
log_level = logging.CRITICAL
elif args.verbose == 2:
log_level = logging.INFO
elif args.verbose >= 3:
log_level = logging.DEBUG
LOG_FORMAT = "%(levelname).4s %(funcName).10s:%(lineno)-4d| %(message)s"
if args.log_to_file:
logging.basicConfig(
filename="doi_query.log",
filemode="w",
level=log_level,
format=LOG_FORMAT,
)
else:
logging.basicConfig(level=log_level, format=LOG_FORMAT)
info(args)
file_name = args.filename[0]
extension = Path(file_name).suffix[1:]
info(f"** file_name = {file_name}")
info(f"** extension = {extension}")
if file_name.startswith("http"):
url = file_name
if "docs.google.com" in url:
url = url.replace("/edit", "/export")
elif os.path.exists(file_name):
file_path = os.path.abspath(file_name)
info(f"path = {file_path}")
url = f"file://{file_path}"
else:
print(f"ERROR: Cannot find {file_name}")
sys.exit()
info(f"** url = {url}")
content = None
rotate_files(DST_FILE)
# os.remove(DST_FILE) if os.path.exists(DST_FILE) else None
# default is lynx; args.catdoc now removed
if not any(
(
args.lynx,
args.plain,
args.markdown,
args.links,
args.w3m,
args.antiword,
args.docx2txt,
args.pdftotext,
)
):
args.lynx = True
if extension == "md":
extension = "markdown"
extension = "html" if not extension else extension
info(f"** extension = {extension}")
# I prefer to use the programs native wrap if possible
if args.markdown:
content = urlopen(url).read()
wrap = "" if args.wrap else "--wrap=none"
columns = 70
command = [
PANDOC_BIN,
"-f",
f"{extension}",
"-t",
"markdown-simple_tables-pipe_tables-multiline_tables",
"--reference-links",
"--reference-location=block",
"--columns",
f"{columns}",
"-o",
DST_FILE,
]
elif args.plain:
content = urlopen(url).read()
wrap = "" if args.wrap else "--wrap=none"
columns = 70
command = [
PANDOC_BIN,
"-f",
f"{extension}",
"-t",
"plain",
"--columns",
f"{columns}",
"-o",
DST_FILE,
]
elif args.lynx:
wrap = "-width 70" if args.wrap else "-width 1024"
command = [
"lynx",
"-dump",
"-nonumbers",
"-display_charset=utf-8",
url,
]
elif args.links:
wrap = "-width 70" if args.wrap else "-width 512"
command = ["links", "-dump", url]
elif args.w3m:
wrap = "-cols 70" if args.wrap else ""
command = ["w3m", "-dump", "-cols", "70", url]
elif args.antiword:
wrap = "-w 70" if args.wrap else "-w 0"
url = url[7:] # remove 'file://'
command = ["antiword", url]
# elif args.catdoc: # now deprecated, not available on homebrew
# wrap = '' if args.wrap else '-w'
# command = ['catdoc', url]
elif args.docx2txt:
wrap = "" # maybe use fold instead?
command = ["docx2txt.pl", file_name, "-"]
elif args.pdftotext:
wrap = ""
command = ["pdftotext", "-layout", "-nopgbrk", file_name, "-"]
else:
print("ERROR: no conversion program specified")
command[1:1] = wrap.split() # insert wrap args after command
print(f"** command = {command} on {url}")
process = Popen(command, stdin=PIPE, stdout=open(DST_FILE, "w"))
process.communicate(input=content)
if args.wrap or args.quote:
with open(DST_FILE) as f:
new_content = []
for line in f.readlines():
if line.isspace():
line = "\n"
if args.wrap and wrap == "": # wrap if no native wrap
info("wrapping")
line = textwrap.fill(line, 70).strip() + "\n"
if args.quote:
info("quoting")
line = line.replace("\n", "\n> ")
new_content.append(line)
content = "".join(new_content)
if args.quote:
content = "> " + content
with open(DST_FILE, "w") as f:
f.write(content)
os.chmod(DST_FILE, 0o600)
call([VISUAL, DST_FILE])