在我的公司,Excel 是處理資料的主要工具。
我正面臨這種情況。人們在 Excel 中獲取有關必須翻譯成其他語言的產品的資訊。我有一個在 jupyter 中運行的 python 代碼。但是要求用戶在 jupyter 中運行 python 代碼并不方便。
因此,當“輸入”表中的用戶填寫表格然后單擊按鈕并運行 python 代碼并將他們的資料輸入“輸出”表時,我想到了帶有按鈕的 excel 檔案。
我認為這是從https://vbaskill.com/tricks/python-script/放入 VBA
Sub RunPythonScript()
'Procedure to run a Python Script in a VBA Procedure using the shell
'Declaration
Dim objShell As Object 'For the Shell
Dim Pythonexe, PythonScript As String
'Create the Shell Object
Set objShell = VBA.CreateObject("Wscript.Shell")
Pythonexe = """C:\\...\Python\...\python.exe """ 'path of the python.exe
PythonScript = "C:\\...\VBAPython.py" 'path of your Python script
'Run your Python script
objShell.Run Pythonexe & PythonScript 'run takes two arguments
'free variables for memory
Set objShell = Nothing
End Sub
因為我想將資料加載為資料框并將它們作為資料框進行操作,所以我想從 https://www.soudegesu.com/en/post/python/pandas-with-openpyxl/#convert-openpyxl-object-到資料框
from openpyxl import load_workbook
import pandas as pd
# Load workbook
wb = load_workbook('Translation.xlsm')
# load sheet
ws = wb['Input']
# Convert to DataFrame
df = pd.DataFrame(ws.values)
你認為它會起作用嗎?或者我該怎么做才能讓它作業b謝謝
uj5u.com熱心網友回復:
您的代碼應該可以作業,我自己嘗試過沒有錯誤。當然,您需要稍后回傳資料才能看到結果!
或者,您可以使用xlwings。這個包允許 Python 和 Excel 之間的大量互動。這就是您在 VBA 中撰寫運行腳本所需的全部內容(取自 xlwings 檔案的RunPython部分):
Sub HelloWorld()
RunPython "import hello; hello.world()"
End Sub
這是 hello.py 檔案中的示例代碼:
# hello.py
import numpy as np
import xlwings as xw
def world():
wb = xw.Book.caller()
wb.sheets[0].range('A1').value = 'Hello World!'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450721.html
