NEWS

[C#] Hướng dẫn thực thi tập tin python trong winform

[C#] Hướng dẫn thực thi tập tin python trong winform
Đăng bởi: Thảo Meo - Lượt xem: 7559 15:32:53, 10/10/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách làm việc tập tin Python trong lập trình C#, Winform.

[C#] How to Excute Python in Winform

Như các bạn biết, ngôn ngữ lập trình Python có bộ thư viện hết sức là mạnh mẽ: phân tích dữ liệu, nhận dạng...

Và nếu bạn đang có ứng dụng bằng Csharp Winform, bạn muốn gởi thông tin để cho Python xử lý xong trả kết quả về.

Rồi chúng ta sẻ hiển thị thông tin dữ liệu đã xử lý xong của Python lên Winform.

maxresdefault

Ở bài viết này, mình sẽ demo một ứng dụng đơn giản là đọc dữ liệu file excel vào winform thông qua Python.

Tại Python mình sử dụng thư viện read excel to json file, rồi kết quả trả về json mình sẽ decode và hiển thị dữ liệu đó lên trên Winform.

Code python Read Excel:

import pandas, sys
uri_excel_path=sys.argv[1]
sys.stdout.reconfigure(encoding='utf-8')
excel_data_df = pandas.read_excel(uri_excel_path, sheet_name='data')
json_str = excel_data_df.to_json(orient='records')
print(json_str)

Ở code python này: dữ liệu chúng ta truyền vào là đường dẫn tập tin file excel

Và kết quả trả về là file json data.

PYTHON_EXCEL

Full source code C# run python:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoReadExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_browse_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                txt_uri.Text = dlg.FileName;
            }
        }

        private void btn_readexcel_Click(object sender, EventArgs e)
        {
            var jData = ReadExcelFromPython(txt_uri.Text);
            var table = JsonConvert.DeserializeObject<DataTable>(jData);

            dataGridView1.DataSource = table;
        }

        public string ReadExcelFromPython(string url)
        {

            var command = $@"{Application.StartupPath}
eadexcel.py  ""{url}""";
            Process process = new Process();
            ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.FileName = @"C:Users
guyeAppDataLocalProgramsPythonPython39python.exe";
            startInfo.Arguments = $"{command}";
            startInfo.StandardOutputEncoding = Encoding.UTF8;
            process.StartInfo = startInfo;
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            return output;


        }
    }
}

Chi tiết bạn có thể xem video hướng dẫn step by step:

Thanks for watching!

pass unrar: laptrinhvb.net

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn thực thi tập tin python trong winform
Đăng bởi: Thảo Meo - Lượt xem: 7559 15:32:53, 10/10/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.