3. Why Excel
•
•
•
•
•
Bosses like it
Useful as a data source
More useful with formatting
Input/output source for Pandas
Can be misused: Excel as a database
4. Available Python modules
•
csv.py
Readers and writers in core libs
•
xlwt/xlrd
Mature, stable modules, mainly XLS support
•
openpyxl
Reads and writes XLSX files
•
xlsxwriter
New module from early 2013
6. XlsxWriter
•
•
•
•
•
Write Excel XLSX files only
Doesn’t read or re-write Excel files
Adds many new features
Uses core modules only
Python 2.5, 2.6, 2.7, 3.1, 3.2, 3.3, PyPy, Jython
7. Why another module
•
•
Why not?
•
XlsxWriter adds support for:
Other modules support some but not all
features
charts, autofilters, tables, data validation,
merged cells, rich text, conditional
formatting, defined names, images, cell
comments, sparklines, outlines
8. Getting Started
•
Install:
$ sudo pip install xlsxwriter
•
Clone or fork:
$ git clone git@github.com:jmcnamara/XlsxWriter.git
$ cd XlsxWriter
$ make test
$ sudo python setup.py install
•
Read:
https://xlsxwriter.readthedocs.org
40. Charts
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Add the worksheet data to be plotted.
data = [10, 40, 50, 20, 10, 50]
worksheet.write_column('A1', data)
# Create a new chart object.
chart = workbook.add_chart({'type': 'area'})
# Add a series to the chart.
chart.add_series({'values': '=Sheet1!$A$1:$A$6'})
# Insert the chart into the worksheet.
worksheet.insert_chart('C1', chart)
workbook.close()
54. Autofilters
import xlsxwriter
workbook = xlsxwriter.Workbook('autofilter.xlsx')
worksheet = workbook.add_worksheet()
# Add a format for the headers.
header_format = workbook.add_format({'bold': 1, 'bg_color': '#C6EFCE'})
# Populate the worksheet data.
# See the xlsxwriter docs for a full example.
...
# Make the columns wider.
worksheet.set_column('A:D', 12)
# Format the header row.
worksheet.set_row(0, 20, header_format)
# Set the autofilter.
worksheet.autofilter('A1:D51')
workbook.close()
56. Tables
•
•
•
Group a range of cells into a single entity
Apply a uniform formatting across the cells
Refer to the table in formulas
worksheet.add_table('B3:F7', {options})
58. Data Validation
•
•
•
Restrict data entry to certain ranges
Raise errors/warning within Excel
Allow selection from drop down lists
data_validation(
'B25',
{'validate': 'integer',
'criteria': 'between',
'minimum': 1,
'maximum': 10})
64. Code All the Things!
•
•
•
•
Lots of features
Useful when you need them
Ignore them when you don’t
Plenty of examples and documentation to
get you started
66. How does it work
•
20% of a studio audience guessed Witchcraft
67. How does it work
•
•
•
•
20% of a studio audience guessed Witchcraft
Actually a collection of XML files in a Zip file
Can be unzipped using standard utilities
Even modified in-place (if you are desperate)
68. How does it work
$ unzip -o -d hello_world hello_world.xlsx
Archive:
hello_world.xlsx
inflating: hello_world/[Content_Types].xml
inflating: hello_world/_rels/.rels
inflating: hello_world/docProps/app.xml
inflating: hello_world/docProps/core.xml
inflating: hello_world/xl/sharedStrings.xml
inflating: hello_world/xl/styles.xml
inflating: hello_world/xl/workbook.xml
inflating: hello_world/xl/_rels/workbook.xml.rels
inflating: hello_world/xl/theme/theme1.xml
inflating: hello_world/xl/worksheets/sheet1.xml
69. How does it work
$ unzip -o -d hello_world hello_world.xlsx
Archive:
hello_world.xlsx
inflating: hello_world/[Content_Types].xml
inflating: hello_world/_rels/.rels
inflating: hello_world/docProps/app.xml
inflating: hello_world/docProps/core.xml
inflating: hello_world/xl/sharedStrings.xml
inflating: hello_world/xl/styles.xml
inflating: hello_world/xl/workbook.xml
inflating: hello_world/xl/_rels/workbook.xml.rels
inflating: hello_world/xl/theme/theme1.xml
inflating: hello_world/xl/worksheets/sheet1.xml
70. How does it work
$ unzip -o -d hello_world hello_world.xlsx
Archive:
hello_world.xlsx
inflating: hello_world/[Content_Types].xml
inflating: hello_world/_rels/.rels
inflating: hello_world/docProps/app.xml
inflating: hello_world/docProps/core.xml
inflating: hello_world/xl/sharedStrings.xml
inflating: hello_world/xl/styles.xml
inflating: hello_world/xl/workbook.xml
inflating: hello_world/xl/_rels/workbook.xml.rels
inflating: hello_world/xl/theme/theme1.xml
inflating: hello_world/xl/worksheets/sheet1.xml
71. How does it work
$ unzip -o -d hello_world hello_world.xlsx
Archive:
hello_world.xlsx
inflating: hello_world/[Content_Types].xml
inflating: hello_world/_rels/.rels
inflating: hello_world/docProps/app.xml
inflating: hello_world/docProps/core.xml
inflating: hello_world/xl/sharedStrings.xml
inflating: hello_world/xl/styles.xml
inflating: hello_world/xl/workbook.xml
inflating: hello_world/xl/_rels/workbook.xml.rels
inflating: hello_world/xl/theme/theme1.xml
inflating: hello_world/xl/worksheets/sheet1.xml
$ xmllint --format hello_world/xl/worksheets/sheet1.xml
72. How does it work
$ unzip -o -d hello_world hello_world.xlsx
...
$ xmllint --format hello_world/xl/worksheets/sheet1.xml
79. How does it work
•
•
It works well
XlsxWriter does the hard work so you
don’t have to
80. close()
•
Next time you need to write an Excel file with
Python try XlsxWriter
•
Clone it on Github, submit issues, add stars
http://github.com/jmcnamara/XlsxWriter
•
Read the documentation
https://xlsxwriter.readthedocs.org
PDF tutorial, cookbook and manual