Translating Excel VBA script to Python [closed]

陌路散爱 提交于 2021-02-19 08:46:07

问题


I have the following VBA script:

Sub excelgraphme()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\DRIVE D\graphme\result.txt", Destination:=Range("$A$1"))
        .TextFilePlatform = 866
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Can someone help me with translating with pywin32? And first of all, what should I do with vb statement: WITH END WITH? How i can translate it?


回答1:


It seems you can use pywin32 library for Python. I rewrote the code, but not sure about the value of TextFileTextQualifier. You can see its value (and other variables) by running your VB script.

import win32com.client
excel = win32com.client.Dispatch("Excel.Application")

ws = excel.Workbooks.Add().Worksheets(1)
qtNew = ws.QueryTables.Add( "TEXT;C:\\DRIVE D\\graphme\\result.txt", ws.Range("A1"))

qtNew.TextFilePlatform = 866
qtNew.TextFileStartRow = 1
qtNew.TextFileParseType = 1
qtNew.TextFileTextQualifier = 1
qtNew.TextFileConsecutiveDelimiter = False
qtNew.TextFileTabDelimiter = True
qtNew.TextFileSemicolonDelimiter = False
qtNew.TextFileCommaDelimiter = False
qtNew.TextFileSpaceDelimiter = False
qtNew.TextFileColumnDataTypes = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
qtNew.TextFileTrailingMinusNumbers = True
qtNew.Refresh()



回答2:


Perhaps it is easier to see if I rewrite without the With And End With statements. With is there so that VBA doesn't have to check that the object (whose method is being called) is Nothing.

Sub excelgraphme()
    Dim qtNew As QueryTable
    Set qtNew = ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\DRIVE D\graphme\result.txt", Destination:=Range("$A$1"))

    '* is equivalent but slightly slower because With statement means VBA won't check each line for qtNew=Nothing
    qtNew.TextFilePlatform = 866
    qtNew.TextFileStartRow = 1
    qtNew.TextFileParseType = xlDelimited
    qtNew.TextFileTextQualifier = xlTextQualifierDoubleQuote
    qtNew.TextFileConsecutiveDelimiter = False
    qtNew.TextFileTabDelimiter = True
    qtNew.TextFileSemicolonDelimiter = False
    qtNew.TextFileCommaDelimiter = False
    qtNew.TextFileSpaceDelimiter = False
    qtNew.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    qtNew.TextFileTrailingMinusNumbers = True
    qtNew.Refresh BackgroundQuery:=False


End Sub


来源:https://stackoverflow.com/questions/23540768/translating-excel-vba-script-to-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!