基于python的excel检查和读写软件

news/2024/7/21 5:49:59 标签: python, excel, 开发语言

软件版本:python3.6

窗口和界面gui代码:

class mygui:

    def _init_(self):
        pass

    
    def run(self):

        root = Tkinter.Tk()

        root.title('ExcelRun')
        max_w, max_h = root.maxsize()
        root.geometry(f'500x500+{int((max_w - 500) / 2)}+{int((max_h - 300) / 2)}')  # 居中显示
        root.resizable(width=False, height=False)

        # 标签组件
        label = Tkinter.Label(root, text='from_xlsx:', font=('宋体', 10))
        label.place(x=40, y=35)

        # 输入框控件
        entry_text = Tkinter.StringVar()
        entry = Tkinter.Entry(root, textvariable=entry_text, font=('FangSong', 10), width=40, state='readonly')
        entry.place(x=120, y=38)

        idlabel1 = Tkinter.Label(root, text='ID:', font=('宋体', 10))
        idlabel1.place(x=70, y=58)
        
        fpath1_num1 = Tkinter.StringVar()
        entry2 = Tkinter.Entry(root, textvariable=fpath1_num1, font=('FangSong', 10), width=10)
        entry2.place(x=120, y=58)
        
        idlabel2 = Tkinter.Label(root, text='score:', font=('宋体', 12))
        idlabel2.place(x=240, y=53)

        fpath1_num2 = Tkinter.StringVar()
        entry3 = Tkinter.Entry(root, textvariable=fpath1_num2, font=('FangSong', 10), width=10)
        entry3.place(x=300, y=58)

        label2 = Tkinter.Label(root, text='to_xls:', font=('宋体', 12))
        label2.place(x=40, y=78)
        
        filepath2 = Tkinter.StringVar()
        filepath2entry = Tkinter.Entry(root, textvariable=filepath2, font=('FangSong', 10), width=40, state='readonly')
        filepath2entry.place(x=120, y=78)
        
        id2label1 = Tkinter.Label(root, text='ID:', font=('宋体', 10))
        id2label1.place(x=70, y=98)
        
        filepath2_num1 = Tkinter.StringVar()
        filepath2_num1entry = Tkinter.Entry(root, textvariable=filepath2_num1, font=('FangSong', 10), width=10)
        filepath2_num1entry.place(x=120, y=98)

        id2label2 = Tkinter.Label(root, text='loc:', font=('宋体', 12))
        id2label2.place(x=240, y=93)
        
        filepath2_num2 = Tkinter.StringVar()
        filepath2_num2entry = Tkinter.Entry(root, textvariable=filepath2_num2, font=('FangSong', 10), width=10)
        filepath2_num2entry.place(x=300, y=98)

        button = Tkinter.Button(root, text='选择路径', command=lambda: self.get_path(entry_text,outputtext))
        button.place(x=410, y=30)
        button_path2 = Tkinter.Button(root, text='选择路径', command=lambda: self.get_path(filepath2,outputtext))
        button_path2.place(x=410, y=78)
        
        button2 = Tkinter.Button(root, text='重名检查', command=lambda: self.get_path2(entry_text,outputtext,fpath1_num1))
        button2.place(x=410, y=128)

        button3 = Tkinter.Button(root, text='成绩转移', command=lambda: self.get_path3(\
            entry_text.get(),int(fpath1_num1.get()),int(fpath1_num2.get()),\
            filepath2.get(),int(filepath2_num1.get()),int(filepath2_num2.get()),outputtext))
            
        button3.place(x=410, y=158)
        
        button4 = Tkinter.Button(root, text='名单核查', command=lambda: self.get_path4(\
            entry_text.get(),int(fpath1_num1.get()),\
            filepath2.get(),int(filepath2_num1.get()),outputtext))
            
        button4.place(x=410, y=188)

        outputtext = Text(root, height=20, width=40)
        # 将多行输入文本放入(pack)窗口中
        outputtext.place(x=120, y=120)
        var='a' 
        lst=[chr(ord(var)+i) for i in range(26)]
        for i in range(0,26):
            outputtext.insert("end",lst[i]+'  '+str(i)+'\t')
            if (i+2)%3==1:
                outputtext.insert("end",'\n')
        root.mainloop()

f=mygui()

excel检查和读写代码:

from xlrd import open_workbook
from xlutils.copy import copy
import xlrd
import os
import random

class zuoye__2_excel:
    def run(self,path1,p1num1,p1num2,path2xls,p2num1,p2num2):
        dic={}
        dic2={}

        rb2 = open_workbook(path1)
        rs2 = rb2.sheet_by_index(0)

        nrows = rs2.nrows
        ncols = rs2.ncols
         
        wb = copy(rb2)

        #通过get_sheet()获取的sheet有write()方法
        ws = wb.get_sheet(0)
        #ws.write(0, 0, 'changed!')
        # 
        for i in range(0, nrows):
        #########################################################################################################
          id=rs2.cell_value(i,p1num1)
          id=int(id)
          sc=rs2.cell_value(i,p1num2)
          dic[id]=sc
          #INDEX=rs.cell_value(i,2)
        #########################################################################################################
          #name=rs.cell_value(i,1)
        #########################################################################################################


        rb2 = open_workbook(path2xls)
        rs2 = rb2.sheet_by_index(0)

        nrows2 = rs2.nrows
        ncols2 = rs2.ncols

        wb2 = copy(rb2)

        #通过get_sheet()获取的sheet有write()方法
        ws2 = wb2.get_sheet(0)
        #ws.write(0, 0, 'changed!')

        for i in range(0, nrows2):
          id1=rs2.cell_value(i,p2num1)
          #id3= id1.replace(" ", "")
          if type(id1)==str or not id1>0 :
            continue
          id2=int(id1)
          #id2=''.join([i for i in id3 if  i.isdigit()])
          #name2=rs2.cell_value(i,6)

          #dic2[id2]=i
          #dic[id2]=rs2.cell_value(i,5)


          if  id2  in dic:
        #########################################################################################################
            ws2.write(i,p2num2, dic[id2])
          else:
            print(id2)
        filepath='w'+str(random.randint(10000, 30000))+'.xls'
        if  os.path.exists(filepath):
            print('文件存在!')
            return '文件存在,重新保存!'
        else:
            wb2.save(filepath)
            return '文件保存到:'+filepath+'!';


http://www.niftyadmin.cn/n/5283241.html

相关文章

python实验报告9 数据可视化操作——pandas

我发现很多人都不太清楚这个csv是怎么弄的,其实通俗的讲就是,你不能用excel形式的改后缀名,你必须通过文本文档去改后缀名,并且必须用或者说默认有题头,行与行之间用换行符,但是数据之间得有逗号&#xff0…

模式识别与机器学习(十):梯度提升树

1.原理 提升方法实际采用加法模型(即基函数的线性组合)与前向分步算法。以决策树为基函数的提升方法称为提升树(boosting tree)。对分类问题决策树是二叉分类树,对回归问题决策树是二叉回归树。提升树模型可以表示为决…

Unity DOTS物理引擎的核心分析与详解

最近DOTS发布了正式的版本,同时基于DOTS的理念实现了一套高性能的物理引擎,今天我们来给大家分享和介绍一下这个物理引擎的使用。 Unity.Physics的设计哲学 Unity.Physics是基于DOTS设计思想的一个高性能C#物理引擎的实现, 包含了物理刚体的迭代计算与碰撞检测等查…

centos安装apache2 https php

centos安装apache2 httpsphp apache2的安装包叫httpd apche2下的配置文件都在/etc/httpd/conf。例如:httpd.conf是http的配置文件。php.conf是php的配置文件。ssl.conf是https的配置文件。 1, 安装 有时候安装anaconda的时候是顺带安装了httpd的。 yum list in…

2312llvm,07clang静态分析器

Clang静态分析器 理解静态分析器 在总体LLVM设计中,如果项目操作原始的(C/C)源码,就属于Clang前端,因为根据LLVMIR恢复源码层信息是很难的. 基于Clang的最有意思工具之一是Clang静态分析器,类似传统编译器的警告,在更小范围中,它用一套检查器来生成详细的漏洞报告. 每个检查…

Git指令集合

文章目录 1. 撤销暂存区内容2.修改commit comment3. 合并分支3.1 git rebase3.2 git merge 4.撤销前3次commit指令5. cherry pick git指令有很多,常用的也就是那么几个,今天回顾一下几个准常用,很重要的几个指令。 1. 撤销暂存区内容 当我把…

MySQL报错:1366 - Incorrect integer value: ‘xx‘ for column ‘xx‘ at row 1的解决方法

我在插入表数据时遇到了1366报错,报错内容:1366 - Incorrect integer value: Cindy for column name at row 1,下面我演示解决方法。 根据上图,原因是Cindy’对应的name字段数据类型不正确。我们在左侧找到该字段所在的grade_6表&…

Go中的Context是什么?

在 Go 编程语言(通常称为 Golang)中,术语 "上下文 "指的是上下文包及其定义的上下文类型。上下文包用于跨 API 边界和进程间传输截止日期、取消信号和其他请求范围值。 上下文包的主要目的是管理并发或分布式系统中操作的生命周期…