前言:
将Excel中列数据以特定的格式另存为TXT中,方便其他软件格式导入
如需要:“数据列A”+ “,”+ “数据列B” 格式
实现:
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import datetime
import pandas as pd
import numpy as np
pd.set_option('display.width', 100)
pd.set_option('precision', 0)
pd.set_option('expand_frame_repr', False)
def env_show1():
messagebox.showinfo(title = "反馈",message= "by 张繁同学 \n意见邮箱:daji_1967@163.com \nbilibili:点星星的灯")
#---------文件选框-------------
def selectFile():
global filepath
filepath = filedialog.askopenfilename(filetypes = [('XLSX','*.xlsx'),('XLS','*.xls'),('ALL FILES','*')])
select_path.set(filepath)
#文件打开
def dif():
file1 = pd.read_excel(filepath,
header=0, # 指定行作为头
#sheet_name=1, # 可表示为 sheet_name=‘sheet’具体名字;也可以sheet_name = [0,1]
#usecols="A:J",
)
num_row, num_colu = file1.shape # 获取当前最大行列
#文件新建在当前.PY文件所在的文件夹
with open('./简易清单{}.txt'.format(datetime.datetime.now().strftime("%H_%M_%S")), encoding='utf-8', mode='w') as w1:
w1.write('Code,Name\n')
for i in range(num_row):
a = list(file1['姓名'])
b = list(file1['考试编号'])
w1.write('{},{}\n'.format(b[i].replace(' ',''), a[i]))
messagebox.showinfo("Info", " 完成!")
win = tk.Tk()
win.resizable(True, True) # 窗口大小可调(长 /宽)
# 获取当前分辨率
screenwidth = win.winfo_screenwidth()
screenheight = win.winfo_screenheight()
# print(type(screenheight))
win.geometry('450x200+{}+{}'.format(int(screenwidth / 3), int(screenheight / 3)))
win.title('Excel转txt')
win.attributes("-alpha",1)#设置透明度
# canvas = tk.Canvas(win,bg = 'pink')
# canvas.pack()
select_path = tk.StringVar()
but1 = tk.Button(win, text='文件选择', command=lambda: selectFile())
but1.place(x=300, y=50, width=100, height=20)
but2 = tk.Button(win, text='转txt', command=lambda: dif())
but2.place(x=300, y=110, width=100, height=20)
but2 = tk.Button(win, text='2')
entry1 = tk.Entry(win, textvariable=select_path)
entry1.place(x=50, y=50, width=240, height=20) # 大小调节放到这
entry2 = tk.Entry(win, textvariable=None,state = 'disabled')
entry2.place(x=50, y=80, width=240, height=20) # 大小调节放到这
entry3 = tk.Entry(win, textvariable=None,state = 'disabled')
entry3.place(x=50, y=110, width=240, height=20) # 大小调节放到这
menubar = tk.Menu(win) #创建一个父容器
#先添加下拉窗口
# menubar.add_command(label = "Quit", command = win.quit)
# menubar.add_command(label = "Open", command = lambda :None)
win['menu'] = menubar
win.mainloop()
效果图:
→
学一点,记一点。鱼翻藻鉴,鹭点烟汀。