python excel表格合并 (带界面程序) pyqt5 QT界面

先不多说先上代码

资源下载地址

点击进入
https://download.csdn.net/download/weixin_50123771/13725304

from PySide2.QtWidgets import QApplication, QMessageBox, QFileDialog
from PySide2.QtUiTools import QUiLoader
import os,xlwt,xlrd,time
import tkinter.filedialog,traceback
from threading import Thread
from PySide2.QtCore import Signal,QObject

#创建一个类来处理信号接收
class MySignals(QObject):
    text_print = Signal(str)

#创建我们要运行的代码
class Stats:
    def __init__(self):
        self.dizhi = os.path.abspath(os.path.dirname(__file__))
        self.ui = QUiLoader().load(f'{self.dizhi}\\untitled.ui')
        #选择目录按钮
        self.ui.pushButton.clicked.connect(self.xzml)

        #选择目录按钮
        self.ui.pushButton_2.clicked.connect(self.tjbt)

        #开始合并-直接链接异步操作
        self.ui.pushButton_3.clicked.connect(self.handleCalc)

        #创建表头list
        self.biaotou=[]

        global_ms.text_print.connect(self.printToGui)
    #选择目录
    def xzml(self):
        filePath = QFileDialog.getExistingDirectory(self.ui, "选择存储路径")
        # print(filePath)
        self.ui.lineEdit.setText(filePath)

        #选择好目录后显示出可以加载文件目录
        wenjian = self.ui.lineEdit.text()
        for dqml,dqzml,name in os.walk(wenjian):
            pass
        wenjianlj=[]#文件路径
        for i in [name for name in name if name.endswith('.xlsx') or name.endswith('.xls')]:
            dz=dqml+'/'+i
            #显示过滤好的文件路径
            global_ms.text_print.emit(str(dz))
            wenjianlj.append(dz)



    #添加表头
    def tjbt(self):
        #有的人会输入中文逗号替换英文逗号
        row_text=self.ui.lineEdit_2.text().replace(',',',')
        #用逗号分隔开得到列表
        row_top = row_text.split(',')

        #遍历表头进属性
        for i in row_top:
            self.biaotou.append(i)

        #将表头显示出来
        self.ui.textBrowser.append(str(row_top))
        self.ui.textBrowser.ensureCursorVisible()

    #开始合并
    def kshb(self):
        #先将所有按钮禁用,防止用户点击操作
        self.ui.pushButton.setEnabled(False)
        self.ui.pushButton_2.setEnabled(False)
        self.ui.pushButton_3.setEnabled(False)
        try:
            #读取输入框文本路径名
            wenjian = self.ui.lineEdit.text()
            # print(text)

            for dqml,dqzml,name in os.walk(wenjian):
                pass

            wenjianlj=[]#文件路径
            for i in [name for name in name if name.endswith('.xlsx') or name.endswith('.xls')]:
                dz=dqml+'/'+i
                # print(dz)
                #显示过滤好的文件路径
                wenjianlj.append(dz)


            excel=[]
            for data in wenjianlj:
                data=xlrd.open_workbook(data)
                sheet=data.sheet_by_index(0)
                
                for i in range(sheet.nrows):
                    if i>0:
                        hang=sheet.row_values(i)
                        excel.append(hang)

            wb=xlwt.Workbook()
            ws=wb.add_sheet('sheet1')

            #读取表格头文件
            data= self.biaotou
            for i,itme in enumerate (data):
                ws.write(0,i,itme)

            global_ms.text_print.emit(str('开始写入'))
            for i,item in enumerate(excel):
                # prin='写入第%d行'%i
                # global_ms.text_print.emit(str(prin))
                for j,val in enumerate(item):
                    ws.write(i+1,j,val)
            wb.save(f'{dqml}\\合并.xls')

            #写入信息到程序界面
            global_ms.text_print.emit(str(f'{dqml}\\合并.xls'))
            global_ms.text_print.emit('写出完成')
        
        except:
            #发生错误将显示再出入日志中
            cuowu=traceback.format_exc()
            global_ms.text_print.emit(str(cuowu))

        #写入完成后将所有按钮打开
        self.ui.pushButton.setEnabled(True)
        self.ui.pushButton_2.setEnabled(True)
        self.ui.pushButton_3.setEnabled(True)

    #创建一个多线任务
    def handleCalc(self):
        #链接到开始合并
        thread = Thread(target=self.kshb)
        thread.start()

    #负责接收信号将文字写入程序界面
    def printToGui(self,text):
        self.ui.textBrowser_2.append(str(text))
        


if __name__ == "__main__":
    #***实例化信号类
    global_ms = MySignals()
       
    #***隐藏tk窗口
    root = tkinter.Tk()  # 创建一个Tkinter.Tk()实例
    root.withdraw()  # 将Tkinter.Tk()实例隐藏

    #***创建主窗口
    app = QApplication([])
    
    stats = Stats()

    stats.ui.show()
    app.exec_()

其中里面包含一个ui文件 代码如下,新建一个txt 将内容如直进行,但后另存为ui即可

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="windowModality">
   <enum>Qt::NonModal</enum>
  </property>
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>494</width>
    <height>683</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <family>楷体</family>
    <pointsize>11</pointsize>
    <weight>50</weight>
    <bold>false</bold>
   </font>
  </property>
  <property name="windowTitle">
   <string>小学生excel合并程序1.0版本-学习QQ群:854740180</string>
  </property>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>20</y>
     <width>471</width>
     <height>81</height>
    </rect>
   </property>
   <property name="title">
    <string>第一步选择目录-学习QQ群:854740180</string>
   </property>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>20</y>
      <width>91</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>选择目录</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>331</width>
      <height>41</height>
     </rect>
    </property>
    <property name="tabletTracking">
     <bool>false</bool>
    </property>
   </widget>
  </widget>
  <widget class="QGroupBox" name="groupBox_2">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>120</y>
     <width>471</width>
     <height>161</height>
    </rect>
   </property>
   <property name="title">
    <string>第二步输入表头-学习QQ群:854740180</string>
   </property>
   <widget class="QTextBrowser" name="textBrowser">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>70</y>
      <width>331</width>
      <height>81</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>331</width>
      <height>41</height>
     </rect>
    </property>
    <property name="inputMask">
     <string/>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="placeholderText">
     <string>表头用逗号隔开例如: 序号,内容,备注</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>360</x>
      <y>20</y>
      <width>91</width>
      <height>131</height>
     </rect>
    </property>
    <property name="text">
     <string>添加</string>
    </property>
   </widget>
  </widget>
  <widget class="QGroupBox" name="groupBox_3">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>300</y>
     <width>471</width>
     <height>131</height>
    </rect>
   </property>
   <property name="title">
    <string>第三步开始合并-学习QQ群:854740180</string>
   </property>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>30</y>
      <width>201</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>开始合并</string>
    </property>
   </widget>
  </widget>
  <widget class="QTextBrowser" name="textBrowser_2">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>440</y>
     <width>471</width>
     <height>221</height>
    </rect>
   </property>
   <property name="documentTitle">
    <string/>
   </property>
   <property name="markdown">
    <string>日志信息显示区域

</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值