Monday, 23 May 2016

Python Script to write a xlsx file

we have a python module called XlsxWriter to write multiple worksheet in MS Excel 2007+ xlsx file. This module can be used to write text, numbers, formulas, hyperlinks and charts.
It supports features such as formatting and many more, including:
  •     100% compatible Excel XLSX files.
  •     Full formatting.
  •     Merged cells.
  •     Defined names.
  •     Charts.
  •     Autofilters.
  •     Data validation and drop down lists.
  •     Conditional formatting.
  •     Worksheet PNG/JPEG images.
  •     Rich multi-format strings.
  •     Cell comments.
  •     Integration with Pandas.
  •     Textboxes.
  •     Memory optimization mode for writing large files.

It supports Python 2.5, 2.6, 2.7, 3.1, 3.2, 3.3, 3.4, 3.5, Jython and PyPy and uses standard libraries only.

Installing XlsxWriter:
Using PIP :
$ sudo pip install XlsxWriter

Using Easy_install:
$ sudo easy_install XlsxWriter

Installing from a tarball:

Download the tarball of latest version and keep it into your working directory and run the following commands to install XlsxWriter module. I have downloaded XlsxWriter-0.8.7.tar.gz

$ tar -zxvf XlsxWriter-0.8.7.tar.gz
$ cd XlsxWriter-0.8.7
$ sudo python setup.py install

# Running a sample program

#!/usr/bin/python
import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('A1', 'Hello world')

workbook.close()




import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

# Insert an image.
worksheet.insert_image('B5', 'logo.png')

workbook.close()

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...