用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('文件写入成功!')