PY+ADO

该脚本使用Python的tkinter库打开文件对话框,让用户选择Excel文件,并通过win32com.client模块读取数据。然后,它执行SQL查询从表格中提取数据,并将结果写入CSV文件,分别创建同频、TDD和FDD的数据集。点击按钮可执行不同的数据筛选操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python
# coding=gbk
import sys, os, datetime,glob,csv
from zt import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow,QTableWidget,QTableWidgetItem,QHeaderView
import   matplotlib.pyplot as plt
from pylab import *


#from sqlalchemy import create_engine




# import tab
# from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtWidgets, QtCore, QtGui


# from PyQt5 import QtCore, QtGui, QtWidgets
class win(QMainWindow, Ui_MainWindow):  ##QMainWindow要导入,Ui_MainWindow对应jk.ui的类名

    def __init__(self):

        super(win, self).__init__()

        self.setupUi(self)
        self.pushButton.clicked.connect(self.on_click_1)
        self.pushButton_2.clicked.connect(self.on_click_2)


        #if self.radioButton.isChecked():


        #elif  self.radioButton_2.isChecked():


    def on_click_1(self):
        import win32com.client, os
        import tkinter as tk
        from tkinter import filedialog
        root = tk.Tk()
        root.withdraw()
        # find_file = filedialog.askdirectory(title="请选择要查找的文件夹")  # 获得选择好的文件夹
        Files_r = filedialog.askopenfilename(title="请选择要筛选的EXCEL表", filetypes=[('EXCEL文件', '*.xlsx')])  # 获得选择好的文件
        if not Files_r:
            print("没有选EXCEL表!")
        else:
            cnn = win32com.client.Dispatch(r'ADODB.connection')
            rs = win32com.client.Dispatch(r'ADODB.Recordset')

            # cnn.Open ("Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source ="+os.getcwd()+"\\")
            cnn.Open("provider=microsoft.ace.oledb.12.0;Extended Properties =excel 12.0;data source=" + Files_r)
            # print("Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source ="+os.getcwd()+"\\")

            sql = "select a.ENBFunctionTDD,a.description,a.refEUtranCellTDD,B.ENBFunctionTDD,B.description from [EUtranRelationTDD$] a,[EUtranCellTDD$] B where a.refEUtranCellTDD <>'' and VAL(a.ENBFunctionTDD)>1 and a.refEUtranCellTDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./同频.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./同频.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                # cnn.Close()
                rs.Close()

                print("同频 exetue is done!")

            sql1 = "select a.ENBFunctionTDD,a.description,a.refExternalEUtranTCellTDD,B.eNBId,B.cellLocalId from [EUtranRelationTDD$] a,[ExternalEUtranTCellTDD$] B where a.refExternalEUtranTCellTDD <>'' and VAL(a.ENBFunctionTDD)>1 and a.refExternalEUtranTCellTDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql1, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./TDD.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./TDD.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                rs.Close()
                print("TDD exetue is done!")

            sql2 = "select a.ENBFunctionTDD,a.description,a.refExternalEUtranTCellFDD,B.eNBId,B.cellLocalId from [EUtranRelationTDD$] a,[ExternalEUtranTCellFDD$] B where a.refExternalEUtranTCellFDD <>'' and VAL(a.ENBFunctionTDD)>1 and a.refExternalEUtranTCellFDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql2, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./FDD.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./FDD.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                rs.Close()
                print("FDD exetue is done!")

    def on_click_2(self):
        import win32com.client, os
        import tkinter as tk
        from tkinter import filedialog
        root = tk.Tk()
        root.withdraw()
        # find_file = filedialog.askdirectory(title="请选择要查找的文件夹")  # 获得选择好的文件夹
        Files_r = filedialog.askopenfilename(title="请选择要筛选的EXCEL表", filetypes=[('EXCEL文件', '*.xlsx')])  # 获得选择好的文件
        if not Files_r:
            print("没有选EXCEL表!")
        else:
            cnn = win32com.client.Dispatch(r'ADODB.connection')
            rs = win32com.client.Dispatch(r'ADODB.Recordset')

            # cnn.Open ("Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source ="+os.getcwd()+"\\")
            cnn.Open("provider=microsoft.ace.oledb.12.0;Extended Properties =excel 12.0;data source=" + Files_r)
            # print("Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source ="+os.getcwd()+"\\")

            sql = "select a.ENBFunctionFDD,a.description,a.refEUtranCellFDD,B.ENBFunctionFDD,B.description from [EUtranRelation$] a,[EUtranCellFDD$] B where a.refEUtranCellFDD <>'' and VAL(a.ENBFunctionFDD)>1 and a.refEUtranCellFDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./FDD同频.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./FDD同频.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                # cnn.Close()
                rs.Close()

                print("FDD同频 exetue is done!")

            sql1 = "select a.ENBFunctionFDD,a.description,a.refExternalEUtranCellTDD,B.eNBId,B.cellLocalId from [EUtranRelation$] a,[ExternalEUtranCellTDD$] B where a.refExternalEUtranCellTDD <>'' and VAL(a.ENBFunctionFDD)>1 and a.refExternalEUtranCellTDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql1, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./FDD_TDD.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./FDD_TDD.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                rs.Close()
                print("FDD_TDD exetue is done!")

            sql2 = "select a.ENBFunctionFDD,a.description,a.refExternalEUtranCellFDD,B.eNBId,B.cellLocalId from [EUtranRelation$] a,[ExternalEUtranCellFDD$] B where a.refExternalEUtranCellFDD <>'' and VAL(a.ENBFunctionFDD)>1 and a.refExternalEUtranCellFDD=b.MOI"
            # s = cnn.Execute(sql)
            rs.Open(sql2, cnn, 1, 3)

            # print(rs.Fields.count)
            # print(rs.RecordCount)
            if rs.RecordCount == 0:
                sys.exit()
            else:
                with open('./FDD_FDD.csv', 'w', encoding='GBK')   as out_f:

                    for i in range(0, rs.Fields.count):
                        out_f.write(rs.Fields(i).Name + chr(44))
                    out_f.write('\n')
                    out_f.closed

                with open('./FDD_FDD.csv', 'a', encoding='GBK')  as out_f:
                    rs.MoveFirst()
                    num = 1
                    while not rs.EOF:
                        for j in range(0, rs.Fields.count):
                            out_f.write(chr(34) + str(rs.Fields.Item(j).Value) + chr(34) + chr(44))
                        out_f.write('\n')
                        rs.MoveNext()
                rs.Close()
                print("FDD_FDD exetue is done!")




if __name__ == '__main__':
    #QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    # MainWindow = QMainWindow()
    w = win()
    w.show()
    # ui = Ui_MainWindow()
    # ui.setupUi(MainWindow)
    # MainWindow.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_44390640

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值