Automate the conversion of txt to xls

I am looking for the cheapest way to automate the conversion of all text files (tab-delimited) in the folder structure to the .xls format, keeping the shape of the columns and rows as they are.

I am currently on MacOS. Linux and Windows are available.

Edit:

import xlwt
import xlrd
f = open('Text.txt', 'r+')
row_list = []
for row in f:
    row_list.append(row.split())
column_list = zip(*row_list)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
i = 0 
for column in column_list:
    for item in range(len(column)):
        worksheet.write(item, i, column[item])
    workbook.save('Excel.xls')
    i+=1

It must...

+5
source share
2 answers

The easiest way is to simply rename all files from * .txt to * .xls. Excel automatically splits the data, keeping the original form.

I will not write your code for you, but here is the beginning:

+1

?

import xlwt
textfile = "C:/Users/your_path_here/Desktop/test.txt"

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False        


style = xlwt.XFStyle()
style.num_format_str = '#,###0.00'  

#for textfile in textfiles:
f = open(textfile, 'r+')
row_list = []
for row in f:
    row_list.append(row.split('|'))
column_list = zip(*row_list)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
i = 0
for column in column_list:
    for item in range(len(column)):
        value = column[item].strip()
        if is_number(value):
            worksheet.write(item, i, float(value), style=style)
        else:
            worksheet.write(item, i, value)
    i+=1
workbook.save(textfile.replace('.txt', '.xls'))

, ...

0

All Articles