Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Xlwt more than 256 columns

Tags:

python

I am importing text files into excel using xlwt module. But it allows only 256 columns to be stored. Are there any ways to solve this problem?

like image 495
Ricky Avatar asked Oct 05 '11 08:10

Ricky


5 Answers

xlwt supports creation of XLS files of the kind created by Excel 97-2003 and read by Excel 97 onwards. The file format is limited to 256 columns and 65536 rows. No amount of changing 256 to some other number in the xlwt source code will change that.

You have 3 options, in increasing order of complexity:

(1) as suggested by another, write a CSV file.

(2) openpyxl ... Excel 2007+ xlsx/xlsm format

(3) win32 COM (Windows only)

like image 81
John Machin Avatar answered Nov 14 '22 00:11

John Machin


Line 8 in the file Column.py in the xlwt package sets the column limit to 256:

raise ValueError("column index (%r) not an int in range(256)" % colx)

You could change this number in the xlwt source to allow more columns, but this will break compatibility with older (edit: ALL) versions of excel.

EDIT The xlwt maintainer says that this will not work, so don't try to do it (I'll leave this answer here as I think it's useful to have the warning).

like image 2
Michael Dunn Avatar answered Nov 14 '22 01:11

Michael Dunn


use xlsxwriter and pandas.

import xlsxwriter, pandas

writer = pandas.ExcelWriter(file, engine='xlsxwriter')
pandas.to_excel(writer, sheet_name='Sheet1')
writer.save()
like image 2
qode Avatar answered Nov 14 '22 01:11

qode


Is that a statement of fact or should xlwt support more than 256 columns? What error do you get? What does your code look like?

If it truly does have a 256 column limit, just write your data in a csv-file using the appropriate python module and import the file into Excel.

like image 1
Fredrik Pihl Avatar answered Nov 13 '22 23:11

Fredrik Pihl


import pandas as pd

writer = pd.ExcelWriter('..\output\The file.xlsx',engine='xlsxwriter') 

file.to_excel(writer,'output_sheet') 

writer.save()
like image 1
Newbie Avatar answered Nov 14 '22 00:11

Newbie