import xlrd def strs(row): try: values = ""; for i in range(len(row)): if i == len(row) - 1: values = values + str(row[i]) else: values = values + str(row[i]) + "|" return values except: raise def xls_txt(xls_name,txt_name): try: data = xlrd.open_workbook(xls_name) sqlfile = open(txt_name,"a") table = data.sheets()[0] nrows = table.nrows for ronum in range(0, nrows): row = table.row_values(ronum) values = strs(row) + ' ' sqlfile.writelines(values) sqlfile.close() except: pass xls_name = 'D:/share/DemarcateResult.xls' txt_name = 'D:/share/DemarcateResult.txt' xls_txt(xls_name,txt_name)
使用前先确保环境有xlrd模块
程序执行后得到的txt文件默认编码格式是Unicode编码,可以使用Notepad++工具进行转化。