P
P
pcdesign2015-03-19 16:55:59
Python
pcdesign, 2015-03-19 16:55:59

How to fix excel file generation with a large number of columns?

import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("list1")
y = 0 
for num in range(5000):
    txt = 'привет' + str(num)
    sheet.write(y, 0, txt)
    sheet.write(y, 1, txt)
    y = y + 1
workbook.save(file.xls)

And I get this error in Excel:
27614a7f82e74c898f67b169fb14f680.png
And if you make a smaller number of lines:
for num in range(5):
Then everything opens and works.
How to win it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lPolar, 2015-03-22
@pcdesign

I'll put in my five cents.
Of course, a native lib for Excel is good, but there is also numpy + pandas:

import numpy as np
import pandas as pd
random_df = pd.DataFrame(np.random.uniform(0,1,size=(5000,5000))) #создаем df 5к x 5к
random_df.to_excel('4k.xlsx') #записать данные в файл.

K
Konstantin Shilov, 2015-03-19
@Fromer

xlwt is an xls format that has a column limit of 256. Try the xlsx format - it has 16 thousand columns of
the library:
github.com/leegao/pyXLSX
github.com/staale/python-xlsx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question