Pywin32处理word另存为

        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                    =  0
wdFormatDocument97                  =  0
wdFormatDocumentDefault             = 16
wdFormatDOSText                     =  4
wdFormatDOSTextLineBreaks           =  5
wdFormatEncodedText                 =  7
wdFormatFilteredHTML                = 10
wdFormatFlatXML                     = 19
wdFormatFlatXMLMacroEnabled         = 20
wdFormatFlatXMLTemplate             = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML                        =  8
wdFormatPDF                         = 17
wdFormatRTF                         =  6
wdFormatTemplate                    =  1
wdFormatTemplate97                  =  1
wdFormatText                        =  2
wdFormatTextLineBreaks              =  3
wdFormatUnicodeText                 =  7
wdFormatWebArchive                  =  9
wdFormatXML                         = 11
wdFormatXMLDocument                 = 12
wdFormatXMLDocumentMacroEnabled     = 13
wdFormatXMLTemplate                 = 14
wdFormatXMLTemplateMacroEnabled     = 15
wdFormatXPS                         = 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)

你可能感兴趣的:(html,python,OS,VBA)