- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[C#] Hướng dẫn thực thi tập tin python trong winform
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.
Ở 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.
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