Я пытаюсь создать SQL -запрос в Excel, загрузить его через VBA в сценарий Python, запустить SQL через Python для получения данных и копировать значения обратно в документ Excel. Код Python работает и принимает запрос как sys.argv [1] , но когда я запускаю следующий сценарий, появляется CMD -поле, а затем немедленно закрывается. Я положил время. Sleep (10) в сценарий Python, чтобы определить, является ли это проблемой Python, но считает, что это VBA. Есть предложения? < /P>
Public Function LoadSQLFromCell(cellReference As String, sheetReference As String, outputCell As String, outputSheet As String)
Dim conn As Object
Dim rs As Object
Dim sqlQuery As String
Dim ws As Worksheet
Dim cell As Range
Dim objShell As Object
Dim PythonExePath As String
Dim PythonScriptPath As String
' Set the worksheet where the SQL query is stored
Set ws = ThisWorkbook.Sheets(sheetReference)
' Get the cell reference
Set cell = ws.Range(cellReference)
' Read the SQL query from the specified cell
sqlQuery = cell.Value
' Escape double quotes in the SQL query
sqlQuery = Replace(sqlQuery, """", """""")
' Set the paths for Python executable and script
PythonExePath = """C:\Program Files\Python39\python.exe"""
PythonScriptPath = """C:\Program Files\localData\updater_sysinput.py"""
' Create a new Wscript.Shell object
queryString = PythonExePath & " " & PythonScriptPath & " """ & sqlQuery & """"
MsgBox queryString
' Run the Python script with the SQL query as input
Set objShell = CreateObject("Wscript.Shell")
objShell.Run queryString, 1, True
' Set the worksheet where the output will be pasted
Set ws = ThisWorkbook.Sheets(outputSheet)
ws.Range(outputCell).Select
ws.Paste
End Function
Подробнее здесь: https://stackoverflow.com/questions/796 ... n-from-vba
Запуск запроса SQL через Python от VBA ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение