Python – Simple writing in Excel with XlsWriter

The XlsxWriter library of Python is a nice gem for a VBA developer – you would be really interested to see the problems a Python developer should be facing, just to achieve a simple part Excel automatization. Still, these are the advantages and the disadvantages of the library:

Advantages:

  • It has a high degree of fidelity with files produced by Excel. In most cases the files produced are 100% equivalent to files produced by Excel.
  • It has extensive documentation, example files and tests.
  • It is fast and can be configured to use very little memory even for very large output files.
  • It supports more Excel features than any of the alternative modules.

Disadvantages:

  • It cannot read or modify existing Excel XLSX files.

Pretty much, it creates xlsx files every time you need them. You do not need to have Excel installed, which is quite ok, if you are a Linux user 🙂 Writing to an Excel spreadsheet is a piece of cake, if you are coming from the VBA world. The structure is the same – Workbook contains Worksheets. Worksheet contain ranges and cells. Ranges contain cells. Pretty much this is it. If you want to install the library, simply write pip install XlsxWriter in the command prompt/terminal.

Then, to refer to the cells you may use the A1  or the RowCol notation. I have created a small example, which adds “Hello World” to a worksheet. Then it loops from 1 to 1000 and writes different values on the frist 3 columns. Like this:

This is the beautiful code, available also in GitHub:

Cheers!

Tagged with: , ,