0

Im creating a Windows Forms Project based on C# using python scripts. But when click the button, which should start a python script i have an error "No module named 'openpyxl'"

private void Save_Click(object sender, EventArgs e)
        {
            StreamWriter fs = new StreamWriter("TxtData.txt", false);

            for (int i = 0; i < data.Length; i++)
            {
                if (i == 2)
                {
                    fs.WriteLine(today);
                }
                else
                {
                    fs.WriteLine(data[i]);
                }
            }

            fs.Close();

            Console.WriteLine("Saved");

            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            engine.ExecuteFile("MainData.py", scope); //Right here
            dynamic load_mas = scope.GetVariable("load_mas");
            dynamic saved = load_mas();
        }

And this is my ython code:

from openpyxl import load_workbook

Pydata = [" ", " ", " ", " ", "Нет данных", "Нет данных", " ", " ", " ",]
# 1 - Справ№, 2 - Рев, 3 - Дата, 4 - Заказчик, 5 - Потребитель, 6 - Проект, 7 - Установка, 8 - Проектант

helper = 7

def load_mas():
    with open('TxtData.txt') as f:
        for i in range(0, 9):
            Pydata[i] = f.readline()

def save_table():
    wb = load_workbook(filename = r'C:\Users\Elisey Kartavyy\Documents\Школа\ООП\Visual Studio\NTVALVE\Shablon.xlsx', data_only = True)
    sheet = wb['Лист1']
    for i in range(len(Pydata)):
        index = 'D' + str(helper)
        sheet[index].value = Pydata[i]
        helper += 1
    wb.save('Shablon.xlsx')

Ive alredy installed openpyxl using pip and IronPython into the Visual Studio 2020

1 Answer 1

0

Based on my test, I reproduced the same problem with you. We need to set search paths for the libraries we want to use.

Please try the following code:

var engine = Python.CreateEngine();
 var scope = engine.CreateScope();
 ICollection<string> searchPaths = engine.GetSearchPaths();
 searchPaths.Add("C:\\Program Files\\IronPython 3.4\\Lib");
 searchPaths.Add("C:\\Program Files\\IronPython 3.4\\Lib\\site-packages");
 engine.SetSearchPaths(searchPaths);
 engine.ExecuteFile("MainData.py", scope); //Right here

Note: I find the openpyxl in the following path:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.