六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

Pywin32处理word另存为

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-15 02:22:56 | 显示全部楼层 |阅读模式
wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")        try:            wordapp.Documents.Open('11.doc')            wordapp.ActiveDocument.SaveAs('11.html', FileFormat=win32com.client.constants.wdFormatFilteredHTML)            wordapp.ActiveDocument.Close()        finally:            wordapp.Quit()

office2007中所支持的格式:
wdFormatDocument                    =  0wdFormatDocument97                  =  0wdFormatDocumentDefault             = 16wdFormatDOSText                     =  4wdFormatDOSTextLineBreaks           =  5wdFormatEncodedText                 =  7wdFormatFilteredHTML                = 10wdFormatFlatXML                     = 19wdFormatFlatXMLMacroEnabled         = 20wdFormatFlatXMLTemplate             = 21wdFormatFlatXMLTemplateMacroEnabled = 22wdFormatHTML                        =  8wdFormatPDF                         = 17wdFormatRTF                         =  6wdFormatTemplate                    =  1wdFormatTemplate97                  =  1wdFormatText                        =  2wdFormatTextLineBreaks              =  3wdFormatUnicodeText                 =  7wdFormatWebArchive                  =  9wdFormatXML                         = 11wdFormatXMLDocument                 = 12wdFormatXMLDocumentMacroEnabled     = 13wdFormatXMLTemplate                 = 14wdFormatXMLTemplateMacroEnabled     = 15wdFormatXPS                         = 18
单元格另存为,因为没找到部分另存为的vba代码,所以调用copy&paste曲线解决
    def saveCell(self, savePath, cell):        tmpDoc = cell.Application.Documents.Add()        #内容为空则End=Start,如果这时Copy则将会Copy到表格        if not cell.Range.Text[0:len(cell.Range.Text)-2] == '':#不成立时则保存空的doc文件            start = cell.Range.Start            end = cell.Range.End            if end>start:                cell.Range.Document.Range(cell.Range.Start, cell.Range.End-1).Copy()            else:                cell.Range.Document.Range(cell.Range.Start, cell.Range.End).Copy()            tmpDoc.Range(0, 0).Paste()        tmpDoc.SaveAs(sys.path[0]+os.sep+'HTML'+os.sep+savePath, FileFormat=win32.constants.wdFormatFilteredHTML)        tmpDoc.Close(SaveChanges=win32.constants.wdDoNotSaveChanges)
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表