81
Creating Excel files with Python and XlsxWriter John McNamara Emutex Ltd

Creating Excel files with Python and XlsxWriter

Embed Size (px)

DESCRIPTION

Creating Excel files with Python and XlsxWriter. PyCon.ie 2013

Citation preview

Page 1: Creating Excel files with Python and XlsxWriter

Creating Excel files with Python and

XlsxWriter

John McNamaraEmutex Ltd

Page 2: Creating Excel files with Python and XlsxWriter

whoamiJohn McNamara

Software developer at Emutex Ltd

http://www.emutex.com

http://github.com/jmcnamara

Page 3: Creating Excel files with Python and XlsxWriter

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

Page 4: Creating Excel files with Python and XlsxWriter

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

Page 5: Creating Excel files with Python and XlsxWriter

Excel 2003 : xls Excel 2007 : xlsx

Excel File Formats

xlwt

openpyxl

xlsxwriter

xlrd

ReadWrite

Page 6: Creating Excel files with Python and XlsxWriter

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

Page 7: Creating Excel files with Python and XlsxWriter

Why another module

• Why not?

• Other modules support some but not all features

• XlsxWriter adds support for:

charts, autofilters, tables, data validation, merged cells, rich text, conditional formatting, defined names, images, cell comments, sparklines, outlines

Page 8: Creating Excel files with Python and XlsxWriter

Getting Started

• Install: $ sudo pip install xlsxwriter

• Clone or fork: $ git clone [email protected]:jmcnamara/XlsxWriter.git

$ cd XlsxWriter

$ make test

$ sudo python setup.py install

• Read:

https://xlsxwriter.readthedocs.org

Page 9: Creating Excel files with Python and XlsxWriter

Hello World.xlsx

Page 10: Creating Excel files with Python and XlsxWriter

Hello World.xlsximport xlsxwriter

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

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

workbook.close()

Page 11: Creating Excel files with Python and XlsxWriter

Hello World.xlsximport xlsxwriter

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

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

workbook.close()

Page 12: Creating Excel files with Python and XlsxWriter

Hello World.xlsximport xlsxwriter

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

worksheet.write(0, 0, 'Hello world')worksheet.write(2, 1, 'Hello world')

workbook.close()

Page 13: Creating Excel files with Python and XlsxWriter

Hello World.xlsximport xlsxwriter

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

worksheet.write(0, 0, 'Hello world')worksheet.write(2, 1, 'Hello world')worksheet.write('C5', 'Hello world')

workbook.close()

Page 14: Creating Excel files with Python and XlsxWriter

Cell Formatting

Page 15: Creating Excel files with Python and XlsxWriter

Cell Formattingimport xlsxwriter

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

italic = workbook.add_format({'italic': True})bold = workbook.add_format({'bold': True, 'font_color': '#9CB640'})

worksheet.write(0, 0, 'Hello')

workbook.close()

Page 16: Creating Excel files with Python and XlsxWriter

Cell Formattingimport xlsxwriter

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

italic = workbook.add_format({'italic': True})bold = workbook.add_format({'bold': True, 'font_color': '#9CB640'})

worksheet.write(0, 0, 'Hello')worksheet.write(1, 0, 'Hello', italic)

workbook.close()

Page 17: Creating Excel files with Python and XlsxWriter

Cell Formattingimport xlsxwriter

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

italic = workbook.add_format({'italic': True})bold = workbook.add_format({'bold': True, 'font_color': '#9CB640'})

worksheet.write(0, 0, 'Hello')worksheet.write(1, 0, 'Hello', italic)worksheet.write(2, 0, 'Hello', bold)

workbook.close()

Page 18: Creating Excel files with Python and XlsxWriter

Cell Formattingset_font_name()

set_font_size()

set_font_color()

set_bold()

set_italic()

set_underline()

set_font_strikeout()

set_font_script()

set_num_format()

set_locked()

set_hidden()

set_align()

set_center_across()

set_text_wrap()

set_rotation()

set_indent()

set_shrink()

set_text_justlast()

set_pattern()

set_bg_color()

set_fg_color()

set_border()

set_bottom()

set_top()

set_left()

set_right()

set_border_color()

set_bottom_color()

set_top_color()

set_left_color()

set_right_color()

Page 19: Creating Excel files with Python and XlsxWriter

Types

Page 20: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

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

workbook.close()

Page 21: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

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

workbook.close()

Page 22: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')

workbook.close()

Page 23: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)

workbook.close()

Page 24: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)

workbook.close()

Page 25: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)worksheet.write(4, 0, date(2013, 10, 13), date_format)

workbook.close()

Page 26: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)worksheet.write(4, 0, date(2013, 10, 13), date_format)worksheet.write(5, 0, '=PI()')

workbook.close()

Page 27: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)worksheet.write(4, 0, date(2013, 10, 13), date_format)worksheet.write(5, 0, '=PI()')worksheet.write(6, 0, 'http://python.com')

workbook.close()

Page 28: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)worksheet.write(4, 0, date(2013, 10, 13), date_format)worksheet.write(5, 0, '=PI()')worksheet.write(6, 0, 'http://python.com')worksheet.write(7, 0, True)

workbook.close()

Page 29: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write(0, 0, 'Hello world')worksheet.write(1, 0, 'Это фраза на русском!')worksheet.write(2, 0, 123)worksheet.write(3, 0, 123.456)worksheet.write(4, 0, date(2013, 10, 13), date_format)worksheet.write(5, 0, '=PI()')worksheet.write(6, 0, 'http://python.com')worksheet.write(7, 0, True)

workbook.close()

Page 30: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write (0, 0, 'Hello world')worksheet.write (1, 0, 'Это фраза на русском!')worksheet.write (2, 0, 123)worksheet.write (3, 0, 123.456)worksheet.write (4, 0, date(2013, 10, 13), date_format)worksheet.write (5, 0, '=PI()')worksheet.write (6, 0, 'http://python.com')worksheet.write (7, 0, True)

workbook.close()

Page 31: Creating Excel files with Python and XlsxWriter

Typesfrom datetime import dateimport xlsxwriter

workbook = xlsxwriter.Workbook('types.xlsx')worksheet = workbook.add_worksheet()date_format = workbook.add_format({'num_format': 'd mmm yyyy'})

worksheet.write_string (0, 0, 'Hello world')worksheet.write_string (1, 0, 'Это фраза на русском!')worksheet.write_number (2, 0, 123)worksheet.write_number (3, 0, 123.456)worksheet.write_datetime(4, 0, date(2013, 10, 13), date_format)worksheet.write_formula (5, 0, '=PI()')worksheet.write_url (6, 0, 'http://python.com')worksheet.write_boolean (7, 0, True)

workbook.close()

Page 32: Creating Excel files with Python and XlsxWriter

Formulas

Page 33: Creating Excel files with Python and XlsxWriter

Formulasworksheet.write_formula('A1', '=1+2')

worksheet.write_formula('A2', '=A1')

worksheet.write_formula('A3', '{=SUM(B1:C1*B2:C2)}')

worksheet.write_formula('A4', '=VLOOKUP("Acme", A2:D6, 3, FALSE)')

Page 34: Creating Excel files with Python and XlsxWriter

Images

Page 35: Creating Excel files with Python and XlsxWriter

Imagesimport xlsxwriter

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

worksheet.insert_image(0, 0, 'logo.png')

workbook.close()

Page 36: Creating Excel files with Python and XlsxWriter

Conditional Formatting

Page 37: Creating Excel files with Python and XlsxWriter

Conditional Formattingimport xlsxwriter

wb = xlsxwriter.Workbook('conditional_format.xlsx')ws = wb.add_worksheet()

high = wb.add_format({'bg_color': '#FFC7CE', 'font_color': '#9C0006'})low = wb.add_format({'bg_color': '#C6EFCE', 'font_color': '#006100'})

data = [ [88, 25, 33, 23, 67, 13], [24, 100, 20, 88, 29, 33], [6, 57, 88, 28, 10, 26], [73, 78, 1, 96, 26, 45], [36, 54, 22, 66, 81, 90],]

for row, row_data in enumerate(data): ws.write_row(row, 0, row_data)

ws.conditional_format('A1:F5', {'type': 'cell', 'criteria': '>=', 'value': 50, 'format': high})

ws.conditional_format('A1:F5', {'type': 'cell', 'criteria': '<', 'value': 50, 'format': low})

wb.close()

Page 38: Creating Excel files with Python and XlsxWriter

Charts

Page 39: Creating Excel files with Python and XlsxWriter

ChartsAreastackedpercent_stacked

Barstackedpercent_stacked

Columnstackedpercent_stacked

Line

Pie

Radarwith_markersfilled

Scatterstraight_with_markersstraightsmooth_with_markerssmooth

Stock

Page 40: Creating Excel files with Python and XlsxWriter

Chartsimport 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()

Page 41: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'area'})

Page 42: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'bar'})

Page 43: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'column'})

Page 44: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'line'})

Page 45: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'pie'})

Page 46: Creating Excel files with Python and XlsxWriter

Chartschart = workbook.add_chart({'type': 'radar'})

Page 47: Creating Excel files with Python and XlsxWriter

Charts

• Configurability

Page 48: Creating Excel files with Python and XlsxWriter

ChartsStacked chart with captions

Page 49: Creating Excel files with Python and XlsxWriter

ChartsChange chart styles

Page 50: Creating Excel files with Python and XlsxWriter

ChartsAdd trendlines to charts

Page 51: Creating Excel files with Python and XlsxWriter

ChartsFormat data points

Page 52: Creating Excel files with Python and XlsxWriter

ChartsSecondary axes

Page 53: Creating Excel files with Python and XlsxWriter

Autofilters

Page 54: Creating Excel files with Python and XlsxWriter

Autofiltersimport 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()

Page 55: Creating Excel files with Python and XlsxWriter

Tables

Page 56: Creating Excel files with Python and XlsxWriter

Tables

• Group a range of cells into a single entity

• Apply a uniform formatting across the cells

• Refer to the table in formulasworksheet.add_table('B3:F7', {options})

Page 57: Creating Excel files with Python and XlsxWriter

Data Validation

Page 58: Creating Excel files with Python and XlsxWriter

Data Validation

• Restrict data entry to certain ranges

• Raise errors/warning within Excel

• Allow selection from drop down listsdata_validation( 'B25', {'validate': 'integer', 'criteria': 'between', 'minimum': 1, 'maximum': 10})

Page 59: Creating Excel files with Python and XlsxWriter

Cell Comments

Page 60: Creating Excel files with Python and XlsxWriter

Cell Comments

• Add comments to cells

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

worksheet.write_comment('A1', 'This is a comment')

Page 61: Creating Excel files with Python and XlsxWriter

Sparklines

Page 62: Creating Excel files with Python and XlsxWriter

Sparklines

• Mini charts within cells to show trends

• Invented by Edward Tufte

Page 63: Creating Excel files with Python and XlsxWriter

Code All the Things!

• Lots of features

Page 64: Creating Excel files with Python and XlsxWriter

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

Page 65: Creating Excel files with Python and XlsxWriter

How does it work

Page 66: Creating Excel files with Python and XlsxWriter

How does it work

• 20% of a studio audience guessed Witchcraft

Page 67: Creating Excel files with Python and XlsxWriter

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)

Page 68: Creating Excel files with Python and XlsxWriter

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

Page 69: Creating Excel files with Python and XlsxWriter

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

Page 70: Creating Excel files with Python and XlsxWriter

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

Page 71: Creating Excel files with Python and XlsxWriter

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

Page 72: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

Page 73: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row>

</sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 74: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row>

</sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 75: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row>

</sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 76: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row>

</sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 77: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row>

</sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 78: Creating Excel files with Python and XlsxWriter

How does it work$ unzip -o -d hello_world hello_world.xlsx...

$ xmllint --format hello_world/xl/worksheets/sheet1.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<worksheet xmlns="..." xmlns:r="..."> <dimension ref="A1"/> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"/> </sheetViews> <sheetFormatPr defaultRowHeight="15"/>

<sheetData> <row r="1" spans="1:1"> <c r="A1" t="s"> <v>0</v> </c> </row> </sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

Page 79: Creating Excel files with Python and XlsxWriter

How does it work

• It works well

• XlsxWriter does the hard work so you don’t have to

Page 80: Creating Excel files with Python and XlsxWriter

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

Page 81: Creating Excel files with Python and XlsxWriter

Thank You John McNamaraEmutex Ltd