How to run Python and a Excel .csv fil from C#

  • Thread starter Thread starter okrus
  • Start date Start date
O

okrus

Guest
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.

Continue reading...
 
Back
Top