Problems with generation xls files in Python

117 views Asked by At

Good afternoon.

In my project I need to generate xls files by content of the database.

I found for this python library xlwt, but I fased with a problem - how to format the cell so that the cell dimensions adjust to content? I.e. I need, that the cell width stretched, and if still not enough, cell height stretched and the contents came in a few lines.

Library docs very limited. I have looked in the githabe examples, but not found the solution of this problem.

I have wrote to the group on Google, which discusses the work of the xlwt library, but my message have not even posted in access (not passed moderation).

Can someone tell me here?

Or maybe some more convenient tool for solving my problem?

1

There are 1 answers

0
hsvyas4u On

You can style the text like given below in sample code.

import xlwt
import os
import subprocess
from xlwt import Workbook
from  tempfile import NamedTemporaryFile
book = Workbook()
sheet1 = book.add_sheet('Sheet 1',  cell_overwrite_ok = True)
styleText = 'font: name Calibri,height 220;align:vertical center, wrap on;'
style = xlwt.easyxf(styleText)
sheet1.write(0, 0, "This is long long content of cell" , style=style )
file = NamedTemporaryFile(suffix=".xls", delete=False)
book.save(file)
file.close()
path = os.path.abspath(file.name)
subprocess.Popen('start ' + path, shell=True)

The wrap on will wrap the text.