Excel
Extracting Data from HTML with BeautifulSoup
Running a Python script using Excel macros
- Writing the Python script
Create a simple Python script. - Write the Excel VBA macro
Show the developer tab.
ClickFile > Options > Customize Ribbon > select the Developer check box.
Figure 1: Displaying the Developer tab.
Click the Developer tab.
On the far-left side click ‘Visual Basic
Click Insert -> Module
Figure 2:Add VBA script.
Figure 3: This is where you will write your macro.
VBA Code
Public Sub RunPython() Dim objShell As Object Set objShell = VBA.CreateObject("Wscript.Shell") Dim PythonExe As String PythonExe = """C:\Users\Valya\AppData\Local\Programs\Python\Python38\python.exe""" Dim PythonScript As String PythonScript = """C:\Users\Valya\Desktop\Do2\Python\FSTEK\Hello.py""" Dim Command As String Command = PythonExe & " " & PythonScript objShell.Run Command End Sub
-
Run the Python script for Excel
Draw a shape on the sheet (Insert tab > Shapes drop-down > Rectangle shape).
Figure 4: Adding the Rectangle.
Right click on the button and select ‘Assign Macro’.
Figure 5: Assign a Macro to a Shape.
Figure 6: Select the macro you want to assign a button to.
A Python code
print("hello")
input("hello, press enter")