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

  • Question

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

    • Edited by okrus Monday, April 22, 2019 8:47 PM
    Monday, April 22, 2019 8:42 PM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 23, 2019 6:10 AM
    Moderator
  • Thanks for the response. Have seen many examples of how to start Python from C #, but my problem is that both the Excel file and Python must start from my Form. The Excel file is first stored in the same Form, after which I will run the Python script that uses the saved Excel file. No similar solutions or explanations for similar problem cannot be found.
    Tuesday, April 23, 2019 8:41 PM
  • 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. 

    if the first code works, check proc.Arguments in the second code by debugging 

    Tuesday, April 23, 2019 8:59 PM
  • Thanks for the tip. It works now, just had to define the string inside the method.
    Thursday, April 25, 2019 6:56 AM