批处理之格式转换(一):“xlsx”批量转换“txt”格式
作者:互联网
用python将Excel的xlsx格式批量转换为txt格式,直接上代码:
# 加载相应的package
import pandas as pd
import openpyxl
import os
# 输入路径
inpath = "D:/Desktep/xlsx"
# 输出路径
outpath = "D:/Desktep/txt"
# 读取excel文件
for afile in os.listdir(inpath):
if afile[-4:].lower() == 'xlsx':
print(afile)
name = inpath + '/' + afile
# 读取每一个sheet
wb = openpyxl.load_workbook(name)
sheets = wb.sheetnames
for sheet in sheets:
print(sheet)
df = pd.read_excel(name, sheet_name=sheet, header=None)
print('开始写入txt文件...')
# 保存txt文件
df.to_csv(outpath + '/' + afile[:-5] + '_' + sheet + '.txt', header=None, sep=',', index=False)
print('文件写入成功!')
标签:xlsx,转换,name,sheet,print,格式,afile,txt 来源: https://blog.csdn.net/xdy18339162958/article/details/121364380