-
Notifications
You must be signed in to change notification settings - Fork 45
/
wordtohtml.py
53 lines (43 loc) · 1.57 KB
/
wordtohtml.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by xiaoqin00 on 2017/7/11
from win32com import client as wc
import os
from optparse import OptionParser
word = wc.Dispatch('Word.Application')
def wordsToHtml(input,output):
# for path, subdirs, files in os.walk(dir):
# for wordFile in files:
# wordFullName = os.path.join(path, wordFile)
# print "word:" + wordFullName
doc = word.Documents.Open(os.path.abspath(input))
wordFile2 = unicode(input, "gbk")
dotIndex = wordFile2.rfind(".")
if (dotIndex == -1):
print "********************ERROR: 未取得后缀名!"
fileSuffix = wordFile2[(dotIndex + 1):]
if (fileSuffix == "doc" or fileSuffix == "docx"):
# fileName = wordFile2[: dotIndex]
# htmlName = fileName + ".html"
htmlFullName = os.path.join(unicode(os.getcwd(), "gbk"), output)
# htmlFullName = unicode(path, "gbk") + "\\" + htmlName
print "generate html:" + htmlFullName
doc.SaveAs(htmlFullName, 10) #将word转存为html
doc.Close()
word.Quit()
print ""
print "Finished!"
if __name__ == '__main__':
# import sys
#
# if len(sys.argv) != 2:
# print "Usage: python funcName.py rootdir"
# sys.exit(100)
# wordsToHtml(sys.argv[1])
parser=OptionParser(usage='%prog [options]')
parser.add_option('-i','--in',dest='input',help='input file')
parser.add_option('-o','--out',dest='output',help='output file')
(options,args)=parser.parse_args()
input=options.input
output=options.output
wordsToHtml(input,output)