python learning-110- merging of multiple xlsx files (including graphical interface)

Foreword:

      A few days ago, there was a program for merging multiple CSV files (including a graphical interface), but after thinking about it, most of the CSV files are used by programmers or people studying IT. Most people may still use xlsx or xls files. It is used a lot, so today I will share with you a Python program that combines xlsx files. For the convenience of use, I also wrote a graphical interface. The program is compatible with xlsx and xls for common use.

note:

       To use pip to install pandas to read the dependency package xlrd of xlsx files

       pip install xlrd

At the same time, it also gives the command to package into a desktop program.exe

1. Source code

#coding:utf-8
import pandas as pd
import os
import datetime

def xlsx_fileMerage(inputfilePath, outputfile):
    '''
    讲xlsx文件进行合并成CSV文件,兼容xls文件
    :param inputfilePath:
    :param outputfile:
    :return:
    '''
    # 定义列名
    columns = ['姓名', '学校', '学号', '成绩', '四级', '六级', '性别', '985', '211']
    dataframe = pd.DataFrame([columns])
    dataframe.to_csv(outputfile, encoding='utf-8', index=False, header=0)
    # 合并文件
    rightCount = 0
    errorCount = 0
    for file in os.listdir(inputfilePath):
        try:
            data=pd.read_excel(inputfilePath+'/'+file)
            dataframe=pd.DataFrame(data)
            dataframe.to_csv(outputfile,mode='a', index=False, encoding='utf-8', header=0)
            rightCount += 1
        except:
            print(file + '文件有误')
            errorCount += 1
    print('文件合并完成 ',rightCount,'个文件成功',errorCount,'个文件有误')



if __name__ == '__main__':
    inputfilePath = 'data2'
    outputfile='data/合并文件2.csv'
    xlsx_fileMerage(inputfilePath, outputfile)

2. With graphical interface code

#coding:utf-8
import os
import wx
import numpy
import pandas as pd
import datetime
'''xlsx文件合并为CSV文件,图形化界面版'''
#选择文件
def OnOpen(a):
    dialog = wx.DirDialog(None, '选择一个文件目录',os.getcwd())
    if dialog.ShowModal() == wx.ID_OK:
        filename.SetValue(dialog.GetPath())
    dialog.Destroy()

def gettime():
    '''
    :return: 当前时间的规范形式
    '''
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
    return now_time

def csv_fileMarge(a):
    inputfilePath=filename.GetValue()
    outputfile='E:/CSV文件合并/合并文件'+gettime()+'.csv'
    #定义列名
    columns = ['姓名', '学校', '学号', '成绩', '四级', '六级', '性别', '985', '211']
    dataframe = pd.DataFrame([columns])
    dataframe.to_csv(outputfile, encoding='utf-8',index=False, header=0)
    #合并文件
    informationLog = ''
    rightCount=0
    errorCount=0
    for file in os.listdir(inputfilePath):
        try:
            data=pd.read_csv(inputfilePath+'/'+file,encoding='utf-8')
            dataframe = pd.DataFrame(data)
            dataframe.to_csv(outputfile, mode='a', index=False, encoding='utf-8', header=0)
            rightCount+=1
        except:
            # print(file + '文件有误')
            informationLog += file + ' 文件有误, '
            errorCount+=1
    # print('文件合并完成 ',rightCount,'个文件成功',errorCount,'个文件有误')
    informationLog += ' 文件合并完成: ' + str(rightCount) + ' 个文件成功, ' + str(errorCount) + ' 个文件有误 '
    contents.SetValue(informationLog)

def xlsx_fileMerage(a):
    '''
    讲xlsx文件进行合并成CSV文件,兼容xls文件
    :param inputfilePath:
    :param outputfile:
    :return:
    '''
    inputfilePath = filename.GetValue()
    outputfile = 'E:/xlsx文件合并/合并文件' + gettime() + '.csv'
    # 定义列名
    columns = ['姓名', '学校', '学号', '成绩', '四级', '六级', '性别', '985', '211']
    dataframe = pd.DataFrame([columns])
    dataframe.to_csv(outputfile, encoding='utf-8', index=False, header=0)
    # 合并文件
    informationLog = ''
    rightCount = 0
    errorCount = 0
    for file in os.listdir(inputfilePath):
        try:
            data=pd.read_excel(inputfilePath+'/'+file)
            dataframe=pd.DataFrame(data)
            dataframe.to_csv(outputfile,mode='a', index=False, encoding='utf-8', header=0)
            rightCount += 1
        except:
            # print(file + '文件有误')
            informationLog += file + ' 文件有误, '
            errorCount += 1
    # print('文件合并完成 ',rightCount,'个文件成功',errorCount,'个文件有误')
    informationLog += ' 文件合并完成: ' + str(rightCount) + ' 个文件成功, ' + str(errorCount) + ' 个文件有误 '
    contents.SetValue(informationLog)

#------------------------------------------------------------------------------------GUI编程---
#定义程序类对象
app = wx.App()

#创建顶层窗口
win = wx.Frame(None, title='xlsx文件合并 结果目录:E:/xlsx文件合并(先把 E:/xlsx文件合并 文件夹建立) 作者:李洋',size=(650, 450))
#面板
bkg = wx.Panel(win)

loadButton = wx.Button(bkg,label = u'选择文件目录')
saveButton = wx.Button(bkg,label = u'合并')
loadButton.Bind(wx.EVT_BUTTON, OnOpen)
saveButton.Bind(wx.EVT_BUTTON, xlsx_fileMerage)

#显示文件路径
filename = wx.TextCtrl(bkg)
#显示文章抽取结果
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)

hbox = wx.BoxSizer()

hbox.Add(filename, proportion=1,flag=wx.EXPAND)
hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)

vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(contents,  proportion=1, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)


bkg.SetSizer(vbox)


win.Show()
app.MainLoop()
#------------------------------------------------------------------------------------GUI编程---

3. Results display

Guess you like

Origin blog.csdn.net/u013521274/article/details/87898671