India

ZestyBeanz Technologies Pvt Ltd 4th Floor, Nila, Technopark Thiruvananthapuram,
India – 695581
Phone: +91 471 4063254
Fax : +91 471 2700171

   .

ZestyBeanz Technologies Pvt Ltd
61/3236, Manikkath Cross Road
Ravipuram, Kochi, India - 682016
Phone: +91 484 4063254

  UAE

Zesty Labs
Office # 2003, Millennium Plaza Building
Sheikh Zayed Rd, Dubai, UAE
Phone: +971 4333 2222​
Mobile: +971-52-7553466

  Germany​

ZestyBeanz Technologies GmbH
Reuterstraße 1
90408 Nürnberg
Fon: +49 911 4801 444
Fax: +49 911 4801 445

Contact Form


omalbastin's picture

Creating an Excel file or a spreadsheet using Python's xlutils (xlwt) module

A spreadsheet is used to simulates a paper accounting worksheet. It displays multiple cells usually in a two-dimensional matrix or grid consisting of rows and columns. We have so many reporting software s that will simply create spreadsheets. But the problem is, these reporting software s may not support on all systems and also take lot of CPU time. Then arise an idea to use some method to create spreadsheet from my python code. Then I found about the module xlutils in python.

XLUTILS module contain packages for reading from(xlrd) and writing to(xlwt) excel files. Since I only need the package to write to excel file I'm installing the xlwt package.

 

Installation

We can install any python package to our repository using pip, pip is an easy way to install python packages.

To install pip

omal@zbeanz:~$sudo apt-get install python-pip

After installing pip you can install python packages by

omal@zbeanz:~$sudo pip install xlwt

This will install xlwt package to python modules

 Now in our python code,to import the package functionality to the our program.

We can define the title, header, number, date, and normal style using xlwt.easyxf(). For example title style is given below

We can define as many styles we want. We can also define how the border should appear. For example,

After defining the styles we can now define the workbook. Workbook is actually what we view in our spreadsheet. To define workbook,

A spreadsheet may contain more than one sheet, so to add sheets,

Now we can start adding data to the sheet we have created.  To write data to the 'sheet1' use the reference ws and ws.write(row, column, 'Value or text',style). For example:

To change the width and height of a cell,

 We can add complete data to the spreadsheet by writing and changing the height and width according to our need. After completing this we can save the workbook to a buffer. So First define the buffer and then save the workbook to the  buffer. This doesn't complete the process.

Now we have to encode the data in the buffer and write it to a file say 'test.xls'. To encode the data in the buffer import base64 module. Now open the file test.xls to view our excel file we have created.

I have not mentioned the last few steps (from encoding) so that you can try it out and create the excel file.

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo], [[foo]]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.