当前位置:   article > 正文

[Python-Excel](01)Excel列数据提取到TXT文件_xlsx转化成txt python

xlsx转化成txt python

前言:

        将Excel中列数据以特定的格式另存为TXT中,方便其他软件格式导入

如需要:“数据列A”+  “,”+  “数据列B” 格式


 实现:    

  1. import tkinter as tk
  2. from tkinter import filedialog
  3. from tkinter import messagebox
  4. import datetime
  5. import pandas as pd
  6. import numpy as np
  7. pd.set_option('display.width', 100)
  8. pd.set_option('precision', 0)
  9. pd.set_option('expand_frame_repr', False)
  10. def env_show1():
  11. messagebox.showinfo(title = "反馈",message= "by 张繁同学 \n意见邮箱:daji_1967@163.com \nbilibili:点星星的灯")
  12. #---------文件选框-------------
  13. def selectFile():
  14. global filepath
  15. filepath = filedialog.askopenfilename(filetypes = [('XLSX','*.xlsx'),('XLS','*.xls'),('ALL FILES','*')])
  16. select_path.set(filepath)
  17. #文件打开
  18. def dif():
  19. file1 = pd.read_excel(filepath,
  20. header=0, # 指定行作为头
  21. #sheet_name=1, # 可表示为 sheet_name=‘sheet’具体名字;也可以sheet_name = [0,1]
  22. #usecols="A:J",
  23. )
  24. num_row, num_colu = file1.shape # 获取当前最大行列
  25. #文件新建在当前.PY文件所在的文件夹
  26. with open('./简易清单{}.txt'.format(datetime.datetime.now().strftime("%H_%M_%S")), encoding='utf-8', mode='w') as w1:
  27. w1.write('Code,Name\n')
  28. for i in range(num_row):
  29. a = list(file1['姓名'])
  30. b = list(file1['考试编号'])
  31. w1.write('{},{}\n'.format(b[i].replace(' ',''), a[i]))
  32. messagebox.showinfo("Info", " 完成!")
  33. win = tk.Tk()
  34. win.resizable(True, True) # 窗口大小可调(长 /宽)
  35. # 获取当前分辨率
  36. screenwidth = win.winfo_screenwidth()
  37. screenheight = win.winfo_screenheight()
  38. # print(type(screenheight))
  39. win.geometry('450x200+{}+{}'.format(int(screenwidth / 3), int(screenheight / 3)))
  40. win.title('Excel转txt')
  41. win.attributes("-alpha",1)#设置透明度
  42. # canvas = tk.Canvas(win,bg = 'pink')
  43. # canvas.pack()
  44. select_path = tk.StringVar()
  45. but1 = tk.Button(win, text='文件选择', command=lambda: selectFile())
  46. but1.place(x=300, y=50, width=100, height=20)
  47. but2 = tk.Button(win, text='转txt', command=lambda: dif())
  48. but2.place(x=300, y=110, width=100, height=20)
  49. but2 = tk.Button(win, text='2')
  50. entry1 = tk.Entry(win, textvariable=select_path)
  51. entry1.place(x=50, y=50, width=240, height=20) # 大小调节放到这
  52. entry2 = tk.Entry(win, textvariable=None,state = 'disabled')
  53. entry2.place(x=50, y=80, width=240, height=20) # 大小调节放到这
  54. entry3 = tk.Entry(win, textvariable=None,state = 'disabled')
  55. entry3.place(x=50, y=110, width=240, height=20) # 大小调节放到这
  56. menubar = tk.Menu(win) #创建一个父容器
  57. #先添加下拉窗口
  58. # menubar.add_command(label = "Quit", command = win.quit)
  59. # menubar.add_command(label = "Open", command = lambda :None)
  60. win['menu'] = menubar
  61. win.mainloop()

效果图:

  →


学一点,记一点。鱼翻藻鉴,鹭点烟汀。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/837693
推荐阅读
相关标签
  

闽ICP备14008679号