[Python-Excel](01)Excel列数据提取到TXT文件

前言:

        将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()

效果图:

  →


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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值