简体   繁体   English

如何从电子表格中的特定单元格调用Python脚本

[英]How to call Python script from specific cells in a spreadsheet

I have a Python script to interact with a database and give some result. 我有一个Python脚本与数据库交互并给出一些结果。 I create a new sheet in a workbook and save this result. 我在工作簿中创建一个新工作表并保存此结果。

I have a workbook and I want to call this script from all the cells in a particular row in the active sheet of the workbook using the cell value as an argument to the script. 我有一个工作簿,我想使用该单元格的值作为脚本的参数,从该工作簿的活动表中特定行的所有单元格中调用此脚本。

I came to know the following things 我开始了解以下内容
1) Create a VBA macro and call a script from the macro 1)创建一个VBA宏并从该宏调用脚本
2) Insert the macro in the sheet 2)在表格中插入宏

I have zero knowledge about VBA and was not able to find good resources for it. 我对VBA的知识为零,无法为其找到良好的资源。

Can my problem can be solved using Python itself? 我的问题可以使用Python本身解决吗?

Can I call a Python script from some cells in a workbook using cell values as an argument to the script? 我可以使用单元格值作为脚本的参数从工作簿的某些单元格中调用Python脚本吗?

Your question is too broad. 您的问题太笼统了。 The answer is yes. 答案是肯定的。 There are several libraries for it. 它有几个库。 Look into openpyxl , win32com : also called pywin32 , and pandas 查看openpyxlwin32com :也称为pywin32pandas

Of these 3, 在这3个中,
openpyxl is the easiest to use but might not contain the full functionality of Excel VBA. openpyxl是最易于使用的,但可能不包含Excel VBA的全部功能。
win32com is the closest to VBA and most function calls are the same right down to the way arguments are passed into them. win32com是最接近VBA的,并且大多数函数调用在参数传递到它们方面都是相同的。
pandas is the right choice if you're crunching a lot of numbers and mainly using the spreadsheet files as data storage. 如果您要处理大量数字,并且主要使用电子表格文件作为数据存储,则pandas是正确的选择。

EDIT: 编辑:

Screenshot of Excel UI with Command Button: 带有命令按钮的Excel UI屏幕截图:

用于命令按钮的Excel UI

VBA Code: VBA代码:

Public Sub StartExeWithArguments()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim ws As Worksheet
    Set ws = wb.Sheets("Config") 'Have a sheet named "Config"

    'The values here are for passing in the args to a python script that runs as an .exe file
    strProgramName = Trim(ws.Range("B4").Value) '.exe filepath
    strPdfLoc = Trim(ws.Range("B5").Value) 'file being acted on
    strPdfSave = Trim(ws.Range("B6").Value) 'file being created
    strPdfPass = Trim(ws.Range("B7").Value) 'additional argument
    'string concatenation
    strArgument = strPdfLoc & " " & strPdfSave & " " & strPdfPass

    'Checking the cmd line to be passed into the Shell as args for the .exe
    Debug.Print """" & strProgramName & """ " & strArgument & """"
    'How VBA calls the cmd to run
    'Note that """" actually prints one " because " is an escape char and also a string quote in VBA
    Call Shell("""" & strProgramName & """ " & strArgument & """", vbNormalFocus)
End Sub

The above code showcases how you can create a macro to run an .exe and pass in arguments. 上面的代码展示了如何创建一个宏以运行.exe并传递参数。
As for trigger on cell selection , use the example showcased in this link . 至于触发单元格选择 ,请使用此链接中展示的示例。

In order to create an .exe file, use pip3 install pyinstaller and cd your way to the folder containing the .py script, then run pyinstaller --onefile yourscript.py 为了创建一个.exe文件,请使用pip3 install pyinstaller并以cd方式进入包含.py脚本的文件夹,然后运行pyinstaller --onefile yourscript.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Google Sheets 电子表格调用 python 脚本的好方法是什么? - What is a good way to call a python script from a Google Sheets spreadsheet? 如何使用 Python 在 Google 电子表格中拆分特定工作表的所有合并单元格? - How do I split all merged cells for a specific sheet within a Google Spreadsheet using Python? 从另一个脚本调用python脚本并获取特定值 - Call python script from another script and get a specific value 从谷歌电子表格执行 Python 脚本 - Execute Python Script from google spreadsheet 如何从 python 进程调用 python 脚本 - How call a python script from a python process Python - 以CSV格式从Excel电子表格打印单个单元格 - Python - Printing individual cells from an Excel spreadsheet in CSV format 如何从C#中的Python脚本调用特定方法? - How do I call a specific Method from a Python Script in C#? 如何从Javascript(jquery / ajax)调用Python脚本中的特定函数/方法 - How can I call a specific function/method in a Python script from Javascript(jquery/ajax) 如何仅从 python 中的特定单元格中抓取数据? - How to webscrape data from only specific cells in python? 使用另一个电子表格中的值将文本添加到空电子表格单元格(使用 Python) - Add text to empty spreadsheet cells using values from another spreadsheet (using Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM