3

I have a text file (textfile.txt) in a folder called DOT and I am trying to convert that file to an Excel file (Excelfile.xls) using Python code. I am not familiar with Python but from other comments I wrote the code below. The code does not work. Could anyone help me get the correct syntax?

book = xlwt.Workbook()
import xlwt
import xlrd
f = open('/DOT/textfile.txt', 'r+')
book.save('/DOT/Excelfile' + '.xls')
5
  • you are not reading your textfile, nor converting it. Can you tell me the data format in the text file? Is it rows of data, with each data point in a row separated by a comma? Commented Apr 11, 2016 at 19:11
  • see the Quickstart section in pypi.python.org/pypi/xlwt Commented Apr 11, 2016 at 19:13
  • The file has rows of data separated by a tab Commented Apr 11, 2016 at 19:26
  • @JamieBull Thanks for the link. I read the information but I am not sure what I need to add/change to make it work. Commented Apr 11, 2016 at 19:56
  • added some code that might be useful Commented Apr 11, 2016 at 19:59

2 Answers 2

2

This is based on documentation from: https://pypi.python.org/pypi/xlwt

You will need to read the file line by line, format it and write it to the xls file.

import xlwt
import xlrd

book = xlwt.Workbook()
ws = book.add_sheet('First Sheet')  # Add a sheet

f = open('/DOT/textfile.txt', 'r+')

data = f.readlines() # read all lines at once
for i in range(len(data)):
  row = data[i].split()  # This will return a line of string data, you may need to convert to other formats depending on your use case

  for j in range(len(row)):
    ws.write(i, j, row[j])  # Write to cell i, j

book.save('/DOT/Excelfile' + '.xls')
f.close()

Here, the data is being read, all the rows at once. Then, each line is being split into a list of data points, and added to a new row in the spreadsheet.

This is not the best/optimal solution but should get you started. Let me know in case there is a bug.

Sign up to request clarification or add additional context in comments.

4 Comments

When I use your code I get error "ValueError: column index (256) not an int in range(256)"
sorry, changed one line: row = data[i].split()
I still get the same error ""ValueError: column index (256) not an int in range(256)". The file has over 500 columns. Could that be the issue?
For .xls files the max number of columns is 256. For xlsx, it is 16,384. You will have to use another library for that, maybe openpyxl from python-excel.org
1

I had a similar problem. The txt file’s content was actually separated by “Tab” blanks (get to know this when importing data in Excel).

Searched and tried some answers, but only got this working fine with mine.

https://mail.python.org/pipermail/tutor/2011-May/083411.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.