软件:本次使用python3.8.1
准备:pandas,openpyxl,xlrd(使用pip安装即可)
import pandas as pd
import openpyxl
import time
import os
#输入路径
inpath = 'D:/Data'
#输出路径
outpath = 'D:/Data'
print ('START:'+ str(time.ctime()))
#读取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('文件写入成功!')
print ('END:' + str(time.ctime()))
本文介绍了一种使用Python批量将Excel文件转换为TXT文件的方法,通过遍历指定目录下的所有.xlsx文件,读取每个工作簿的所有工作表,并将其内容转换为CSV格式的TXT文件,适用于数据迁移或格式转换需求。

2086

被折叠的 条评论
为什么被折叠?



