如何从C#运行Python和Excel .csv文件 [英] How to run Python and a Excel .csv fil from C#

查看:109
本文介绍了如何从C#运行Python和Excel .csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个python脚本arp.py和Excel文件"test90_90.csv。


是否可以直接从C#Forms运行Python脚本和excel文件?我有一个C#表单,我填写一个数据表并将其保存为excel文件
,然后使用一个使用excel文件中的数据的python脚本。此时我从cmd运行文件(使用python中的"click"函数)。 "
PATH >
python arp.py eFile.csv --show"但最好的解决方案是如果我可以直接从C#Forms运行相同的功能。这是可能的,在我的情况下,它必须通过按钮点击事件来完成吗?


这是我到目前为止所尝试的:

 private void runPython_Btn_Click(object sender ,EventArgs e)
{
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName =" C:\\Users \\Surkho Shamilov \\ AppData \\ Local \\Programs \\Python \\Python35-32 \\python.exe英寸;
proc.Arguments =" \" C:\\Users \\Surkho Shamilov \\Documents \\Surkho arbeidsmappe \\Python \\ 3D极地情节python\\arp.py\" " +
" \" C:\\Users \\Surkho Shamilov \\Documents \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \test_90_90_3.csv\" --show英寸;
try
{
var processStart = System.Diagnostics.Process.Start(proc);
Console.WriteLine(processStart);
}
catch(exception ex)
{
Console.WriteLine(ex.Message);
}
}

代码abowe工作正常,除了我必须在运行之前在代码中设置Excel文件的名称。在我的情况下,这不是optimale。 


我尝试了一个文本框,其中可以手动设置文件的名称,但是当我添加字符串时,程序不起作用。我没有得到错误的按摩,当我添加字符串时它就不起作用。 


这就是我的尝试:

 private void runPython_Btn_Click(object sender,EventArgs e)
{
excelFileName = excelFileName_Tbx.Text;
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName =" C:\\Users \\Surkho Shamilov \\ AppData \\ Local \\Programs \\Python \\Python35-32 \\python.exe英寸;
proc.Arguments =" \" C:\\Users \\Surkho Shamilov \\Documents \\Surkho arbeidsmappe \\Python \\ 3D极地情节python\\arp.py\" " +
" \" C:\\Users \\Surkho Shamilov \\Documents \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \" + excelFileName + QUOT; \" --show英寸;
try
{
var processStart = System.Diagnostics.Process.Start(proc);
Console.WriteLine(processStart);
}
catch(exception ex)
{
Console.WriteLine(ex.Message);
}
}


但如果有更好的解决方法,我会非常感激。


解决方案

嗨okrus,


感谢您在此发帖。


对于您的问题,您可以参考以下链接。它展示了如何使用控制台在C#中运行Python。


https://www.red-gate.com/simple-talk/dotnet/net-framework/dynamic-language-integration-in-ac-world/


最好的问候,


Wendy


Assuming I have a python script arp.py and an Excel file "test90_90.csv.

Is it possible to run a Python script and an excel file directly from C# Forms? I have a C # form where I fill a table of data and save it as an excel file and then use a python script that uses the data from the excel file. At this point I run the files from cmd (using "click" functions in python). "PATH> python arp.py eFile.csv --show" But the best solution would be if I could run the same functionality directly from C# Forms. Is this possible, and in my case it has to be done with a button click event?

This is what I have tried so far:

private void runPython_Btn_Click(object sender, EventArgs e)
        {
            System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
            proc.FileName = "C:\\Users\\Surkho Shamilov\\AppData\\Local\\Programs\\Python\\Python35-32\\python.exe";
            proc.Arguments = "\"C:\\Users\\Surkho Shamilov\\Documents\\Surkho arbeidsmappe\\Python\\3D polar plot python\\arp.py\" " +
                "\"C:\\Users\\Surkho Shamilov\\Documents\\Surkho arbeidsmappe\\Python\\3D polar plot python\\test_90_90_3.csv\" --show";
            try
            {
                var processStart = System.Diagnostics.Process.Start(proc);
                Console.WriteLine(processStart);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

The code abowe works fine, except the fact that I have to set the name of the Excel file in the code before I run it. That's not optimale in my case. 

I tried to a textbox where a can set the name of the file manually, but when I add the string, the program don't work. I don't get eany error massages, it just dont work when I add the string. 

This is what I tried:

private void runPython_Btn_Click(object sender, EventArgs e)
        {
            excelFileName = excelFileName_Tbx.Text;
            System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
            proc.FileName = "C:\\Users\\Surkho Shamilov\\AppData\\Local\\Programs\\Python\\Python35-32\\python.exe";
            proc.Arguments = "\"C:\\Users\\Surkho Shamilov\\Documents\\Surkho arbeidsmappe\\Python\\3D polar plot python\\arp.py\" " +
                "\"C:\\Users\\Surkho Shamilov\\Documents\\Surkho arbeidsmappe\\Python\\3D polar plot python\\"+excelFileName+"\" --show";
            try
            {
                var processStart = System.Diagnostics.Process.Start(proc);
                Console.WriteLine(processStart);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

But if there is better ways solving the problem, I'd greatly appreciate it.

解决方案

Hi okrus,

Thank you for posting here.

For your question, you could refer to the link below. It shows how to run Python in C# with console.

https://www.red-gate.com/simple-talk/dotnet/net-framework/dynamic-language-integration-in-a-c-world/

Best Regards,

Wendy


这篇关于如何从C#运行Python和Excel .csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆