Python3でexcelファイルからテキストファイル(*txt)に変換するサンプルコード

サンプルコード:
import os
import glob
import xlrd3 as xlrd
import re

path = os.getcwd()
files = glob.glob('sourceExcel/*’)

for file in files:
wb = xlrd.open_workbook(file)

for sheetName in wb.sheet_names():
txtFile = open('outputTxts/’ + sheetName + '.txt’, mode=’w’, encoding=’utf-8′)
sheet = wb.sheet_by_name(sheetName)
for rownum in range(sheet.nrows):
v1 = sheet.cell(rownum, 0).value
if (type(v1) == float):
v1 = str(v1)
v1 = re.sub('\.0*$’, “", v1)
v1 = v1.rstrip()
v2 = sheet.cell(rownum, 1).value
if (type(v2) == float):
v2 = str(v2)
v2 = re.sub('\.0*$’, “", v2)
v2 = v2.rstrip()
dataStr = v1 + '=’ + v2 + '\n’
txtFile.write(dataStr)
txtFile.close()

Python

Posted by arkgame