forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordview
More file actions
executable file
·57 lines (48 loc) · 2.15 KB
/
wordview
File metadata and controls
executable file
·57 lines (48 loc) · 2.15 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
56
#!/usr/bin/env python
# Convert a doc or docx file to html, and call a new firefox window on it.
import sys, os
import subprocess
import magic
import time
magic_parser = None
def view_word_files(file_list):
outfiles = []
for filename in file_list:
pathparts = os.path.split(filename)
fileparts = os.path.splitext(pathparts[1])
outfilename = os.path.join("/tmp", fileparts[0] + ".html")
# Extra credit: could use tempfile.mkstemp
# wvHtml does a pretty good job with simple .doc files.
if filename.lower().endswith(".doc"):
subprocess.call(["wvHtml", filename, outfilename])
outfiles.append(outfilename)
# docx is harder to convert to HTML. Use libreoffice's unoconv.
elif filename.lower().endswith(".docx"):
subprocess.call(["unoconv", "-f", "html", "-o", outfilename,
filename])
outfiles.append(outfilename)
else:
global magic_parser
if not magic_parser:
magic_parser = magic.open(magic.MAGIC_MIME)
magic_parser.load()
mimetype = magic_parser.file(filename)
print "mime type is", mimetype
if mimetype.startswith("application/msword"):
subprocess.call(["wvHtml", filename, outfilename])
outfiles.append(outfilename)
elif mimetype.startswith("application/vnd.openxmlformats-officedocument.wordprocessingml.document"):
subprocess.call(["unoconv", "-f", "html", "-o", outfilename,
filename])
outfiles.append(outfilename)
if outfiles:
subprocess.call(["firefox", "-new-window", outfiles[0]])
for f in outfiles[1:]:
# If we don't wait for the new window to pop up before
# calling new-tab, bad things will happen: the document
# may load in a new tab in the old window and THEN pop up
# an unwanted third window. Go firefox.
time.sleep(1)
subprocess.call(["firefox", "-new-tab", f])
if __name__ == "__main__":
view_word_files(sys.argv[1:])