Home » excel » vb.net – Translating Excel VBA script to Python

vb.net – Translating Excel VBA script to Python

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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()

Answer:

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