工具箱相关
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HBexcel.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import xlrd
  3. import tkinter as tk
  4. from tkinter import filedialog as tkFileDialog
  5. from tkinter import messagebox
  6. from openpyxl import load_workbook,workbook
  7. def start():
  8. window = tk.Tk() # 创建窗口对象的背景色
  9. window.title('excel合并程序') # 设置窗口的标题
  10. window.geometry('400x250') # 设置窗口的大小
  11. title = tk.Label(window, text='excel合并程序',
  12. font=('微软雅黑', 12), width=40, height=3)
  13. title.place(x=0, y=0, anchor='nw')
  14. choosepath = tk.Label(window, text='选择表格文件夹:',
  15. font=('微软雅黑', 12), width=20, height=2)
  16. choosepath.place(x=0, y=50, anchor='nw')
  17. path_var1 = tk.StringVar() # 输入路径
  18. entry1 = tk.Entry(window, textvariable=path_var1)
  19. entry1.place(x=170, y=65, anchor='nw')
  20. resultpath = tk.Label(window, text='选择输出文件夹:',
  21. font=('微软雅黑', 12), width=20, height=2)
  22. resultpath.place(x=8, y=100, anchor='nw')
  23. path_var2 = tk.StringVar() # 输出路径
  24. entry2 = tk.Entry(window, textvariable=path_var2)
  25. entry2.place(x=170, y=115, anchor='nw')
  26. def choose1():
  27. file_dir = tkFileDialog.askdirectory()
  28. path_var1.set(file_dir)
  29. def choose2():
  30. file_dir2 = tkFileDialog.askdirectory()
  31. path_var2.set(file_dir2)
  32. def tuichu():
  33. window.destroy()
  34. def main():
  35. inpath = entry1.get()
  36. outpath = entry2.get()
  37. #输出路径
  38. outfpath = outpath + '/UnionResult.xlsx'
  39. new_wb = workbook()
  40. ws2 = new_wb['Sheet']
  41. new_wb.remove(ws2)
  42. #遍历文件夹
  43. for parent,dirname,filenames in os.walk(inpath):
  44. for filename in filenames:
  45. #排除其他文件的干扰
  46. if '.xls' in filename:
  47. #完整路径
  48. filepath = parent + '/' +filename
  49. #记下名字
  50. fname = str(filename)
  51. dmm = fname.replace('.xls','')
  52. #读取
  53. xls = xlrd.open_workbook(filepath)
  54. sheet1 = xls.sheets()[0]
  55. rows = sheet1.nrows
  56. ii = 0
  57. while ii < rows:
  58. data = sheet1.row(ii)
  59. if '.xlsx' in filename:
  60. #完整路径
  61. filepath = parent + '/' +filename
  62. #记下名字
  63. fname = str(filename)
  64. dmm = fname.replace('.xlsx','')
  65. old_wb = load_workbook(filepath)
  66. new_wb = load_workbook(outfpath)
  67. messagebox.showinfo("消息","运行成功")
  68. window.destroy()
  69. tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
  70. tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
  71. tk.Button(window,text='确认',command=main).place(x=120,y=210,anchor='nw')
  72. tk.Button(window,text='取消',command=tuichu).place(x=220,y=210,anchor='nw')
  73. window.mainloop()
  74. if __name__ == '__main__':
  75. start()