python - 在 Windows 窗体中使用 xlrd 和 Ironpython
问题描述
我对 Windows 窗体和 Ironpython 很陌生,我必须连接一个 python 脚本来读取 excel 文件的列值并将列表返回到 Windows 窗体,所以我为此做了一个类:
class Python_Script
{
private ScriptEngine engine;
private ScriptScope scope;
private ScriptSource source;
public Python_Script(string name)
{
engine = Python.CreateEngine();
scope = engine.CreateScope();
string str = @"script_route" + name;
engine.ExecuteFile(@str, scope);
}
public List<string> Col_Values(int sheet, int col)
{
List<string> result;
dynamic col_values = scope.GetVariable("Col_Values");
result = col_values(sheet, col);
return result;
}
当 mi form 初始化时,我这样做:
public partial class Form2 : Form
{
List<string> test_col = new List<string>();
Python_Script my_py = new Python_Script("script_leer_excel.py");
public Form2()
{
test_col = my_py.Col_Values(0,0);
}
这是我的python脚本
import sys
import xlrd
loc = ("excel_route")
wb = xlrd.open_workbook(loc)
def Col_Values(sheet, col):
sheet = wb.sheet_by_index(sheet)
result = sheet.col_values(col)
return result
但是当我运行代码时出现错误: IronPython.Runtime.Exceptions.ImportException: 'No module named xlrd'
我不知道如何在 Ironpython 中安装 xlrd 模块,我使用 python 3.8 的命令 promt 安装了它,并且我在 Visual strudio 中的 python 环境告诉我我正在运行 python 3.8 Visual Studio python 环境
解决方案
推荐阅读
- r - 尝试使用“xml_find_all”抓取网站后出现错误“xpath_search”
- javascript - 如何修复“TypeError: (images || []).forEach 不是函数”
- python - PyLaTeX:pylatex.errors.CompilerError:未找到 LaTex 编译器
- json - 我应该在组件之间传递 json 还是 dom-nodes?
- c - 是什么导致 C 中出现分段错误(核心转储)?
- javascript - 尝试用 rxjs 创建一个极简的纯 js 应用
- x86 - fetch完成后PC寄存器指向什么?
- c++ - 使用引用进行高效交换?
- c++ - 如何在嵌套的 for 循环中使用 continue for a while 循环
- c# - 为什么一些 unicode 字符在 Visual Studio 属性网格中显示为一个框?