v1103 要把一张工作表嵌入到上百个EXCEL文件中,一分钟搞定

问题描述:要把一张工作表嵌入到上百个EXCEL文件中,一分钟搞定
编程语言:python
编程环境:VS Code
下载见站:领越科创 https://lingyuetech.top
核心代码:

 def filecontents():
            pathcwd2 = os.getcwd() 
            filetypes = (
                ('excel files', '*.xls*'),
                ('All files', '*.*')
            )
            filename = filedialog.askopenfilename(
                title='选择 Excel 文件',
                initialdir=pathcwd2,
                filetypes=filetypes)   # Choose A workbook with fullname and path
            directory2, BSfilename2 = os.path.split(filename) 
            print(BSfilename2)  # 已选源文件全名
            global GV1
            GV1=BSfilename2
            Label(root, text=BSfilename2,anchor="w").place(x=235, y=15,width=350, height=50)

            app2=xw.App(visible=True,add_book=False) # 出现Sheet选择框
            app2.display_alerts=False # 关闭警告信息,提升运行速度
            app2.screen_updating=False
            workbook=app2.books.open(filename,"r+") 
            src_worksheet=workbook.sheets
            src_worksheetNL=workbook.sheet_names

            list_with_tuples = []
            for i in range(len(src_worksheetNL)):
                list_with_tuples.append((i,src_worksheetNL[i]))
            print('list_with_tuples = ',list_with_tuples) 

            site = list_with_tuples
            v = tk.StringVar()  # IntVar() 用于处理整数类型的变量
            j=0
            for name, num in site: # 重构后的写法,也非常简单易懂
                radio_button = tk.Radiobutton(root,bg="yellow",text = num,variable = v,value =num)
                radio_button.pack(anchor ='w')
                radio_button.place(x=250, y=70+j)
                j=j+25
            def selectsheet():
                selected_option = v.get()
                global GV2
                if selected_option!='':
                   GV2=v.get() 
                Label(root, text=GV2,anchor="w").place(x=385, y=190,width=100, height=50)

            sheet_button = tk.Button(root, text="确认选项", command=selectsheet) #缩进表示逻辑,很关键
            sheet_button.place(x=310, y=200,width=55, height=25)
            Label(root, text=GV2,anchor="w").place(x=385, y=190,width=100, height=50)
            app2.quit()

        def findfolder():
            pathcwd3 = os.getcwd() 
            foldername=filedialog.askdirectory(title='选择目标文件夹!', initialdir=pathcwd3)
            Label(root, text=foldername,anchor="w").place(x=235, y=225,width=350, height=50)
            global GV3
            GV3=foldername
        #''' #主函数
        def AddSheetToFiles():
            app=xw.App(visible=True,add_book=False) # 调用xw 库中的APP类,创建了一个Excel软件对象,并赋值给变量app(即一个excel窗口对象),是否显示Excel软件(一般是T),是否创建一个工作簿(一般是F)
            app.display_alerts=False # 关闭警告信息,提升运行速度
            app.screen_updating=False

            src_workbook_name=GV1
            copied_sheet_name=GV2

            deeppathExcel=GV3
            listAllFile=os.listdir(deeppathExcel) #指定目录下的文件列表

            src_workbook=app.books.open(src_workbook_name,"r+")
            src_worksheet=src_workbook.sheets[copied_sheet_name]
            lenoflist=len(listAllFile)

            P=True #判断文件夹是否有Excel文件的指针

            for Filename in listAllFile:
                if Filename.endswith(".xlsx"):
                    P=False
                    Fullname= os.path.join(deeppathExcel, Filename)
                    dest_workbook=app.books.open(Fullname,"r+")
                    dest_worksheet=dest_workbook.sheets
                    dest_worksheetNL=dest_workbook.sheet_names  #工作表名称列表
                    print(dest_worksheetNL)

                    if copied_sheet_name not in dest_worksheetNL:
                        sheet3=dest_workbook.sheets[0]
                        src_worksheet.api.Copy(Before=sheet3.api)
                        dest_workbook.sheets[0].name=copied_sheet_name
                    else:
                        sheet3=dest_workbook.sheets[0]
                        src_worksheet.api.Copy(Before=sheet3.api)
                        dest_workbook.sheets[0].name=copied_sheet_name+"New"
                    
                    dest_workbook.save()
                    dest_workbook.close()

            if P==False:
                print(showinfo(title="工作表添加已完成", message=f" 共有 {lenoflist} 个.xlsx 文件添加了该工作表"))
            else:
                print(showinfo(title="敬  告", message=" 该文件夹下无.xlsx 文件"))     

            src_workbook.save()
            src_workbook.close()

            app.quit()
            root.quit()
            print("Worksheet Creation Done")
        #'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值