Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Show FOSDEM: openpyxl

Eric Gazoni
February 02, 2014

Show FOSDEM: openpyxl

Presenting how to easily use Excel as a container for typed tabular data in Python, performance hints, and a progress status of the library after 3 years of development.

Eric Gazoni

February 02, 2014
Tweet

More Decks by Eric Gazoni

Other Decks in Programming

Transcript

  1. FOSDEM 2014 | What is openpyxl ? Q: Will it

    work on good ol’ XLS files ? ! A: no, but there is xlrd / xlwt for that
  2. FOSDEM 2014 | What is openpyxl ? Emerged in 2010

    from the lack of a viable alternative (at the time at least)
  3. FOSDEM 2014 | What is openpyxl ? Shaped by the

    needs of the scientific world
  4. FOSDEM 2014 | What is openpyxl ? Inspired by the

    sad years of my life when I was writing Excel VBA for a living
  5. FOSDEM 2014 | What is openpyxl ? OOXML is a

    good alternative to CSV …
  6. FOSDEM 2014 | What is openpyxl ? … well, ok,

    it’s an insane, nonsensical format …
  7. FOSDEM 2014 | The awesomesauce Auto type casting (a.k.a. data

    type guessing) ! (questionable feature)
  8. FOSDEM 2014 | Words are cheap, show us some code

    ! An openpyxl crash course 1.
  9. FOSDEM 2014 | Words are cheap, show us some code

    ! Basic usage! (https://openpyxl.readthedocs.org)
  10. FOSDEM 2014 | Words are cheap, show us some code

    ! >>> from openpyxl import Workbook >>> wb = Workbook() >>> ws = wb.create_sheet() >>> ws['A4'] = 4 >>> print ws['A1':'C2'] ((<Cell Sheet1.A1>, <Cell Sheet1.B1>, <Cell Sheet1.C1>), (<Cell Sheet1.A2>, <Cell Sheet1.B2>, <Cell Sheet1.C2>)) >>> wb.save('my_workbook.xlsx')
  11. FOSDEM 2014 | The awesomesauce import datetime from openpyxl import

    Workbook ! wb = Workbook() ws = wb.active # set date using a Python datetime ws['A1'] = datetime.datetime(2010, 7, 21) ! print ws['A1'].style.number_format.format_code # returns 'yyyy-mm-dd' ! # set percentage using a string followed by the percent sign ws['B1'] = '3.14%' ! print ws['B1'].value # returns 0.031400000000000004 ! print ws['B1'].style.number_format.format_code # returns '0%'
  12. FOSDEM 2014 | Words are cheap, show us some code

    ! >>> from openpyxl import Workbook >>> wb = Workbook() >>> ws = wb.create_sheet() >>> for i in range(10): >>> ws.append([i, i]) >>> chart = ScatterChart() >>> xvalues = Reference(ws, (0, 1), (9, 1)) >>> values = Reference(ws, (0, 0), (9, 0)) >>> series = Series(values, xvalues=xvalues) >>> chart.append(series) >>> ws.add_chart(chart)
  13. FOSDEM 2014 | Words are cheap, show us some code

    ! >>> from openpyxl import load_workbook >>> wb2 = load_workbook('test.xlsx') >>> print wb2.get_sheet_names() ['Sheet2', 'New Title', 'Sheet1']
  14. FOSDEM 2014 | Ok, but is it fast ? New

    study shows fast software is sexy 1.
  15. FOSDEM 2014 | Ok, but is it fast ? «

    No », « no » and «eeeh, good enough »
  16. FOSDEM 2014 | Ok, but is it fast ? Default

    mode keeps everything in RAM = memory hog
  17. FOSDEM 2014 | Ok, but is it fast ? Optimized

    mode still spends a lot of time in Python = With more user-friendliness comes more CPU cycles
  18. FOSDEM 2014 | Ok, but is it fast ? However,

    benchmarks are not real life
  19. FOSDEM 2014 | Ok, but is it fast ? Known

    to work well in production environments
  20. FOSDEM 2014 | Ok, but is it fast ? But

    that’s not done yet !
  21. FOSDEM 2014 | Ok, but is it fast ? Tuning

    and benchmarking all the time
  22. FOSDEM 2014 | Ok, but is it fast ? If

    you only write files, try @jmcnamara13’s excellent library XlsxWriter (https://xlsxwriter.readthedocs.org)
  23. FOSDEM 2014 | Pimp your ride A: you’re just having

    fun with a reasonably sized workbook ! Hint: you’re pretty safe
  24. FOSDEM 2014 | Pimp your ride Use row-oriented logic (columns

    are evil) ! def transpose_columns(cols): return map(lambda *a: list(a), *cols)
  25. FOSDEM 2014 | Pimp your ride Don’t hesitate to use

    optimized modes ! (even if you have to rewrite some code)
  26. FOSDEM 2014 | A Co-op game Lots of similarly licensed

    libraries, making cooperation easier
  27. FOSDEM 2014 | A Co-op game PHPExcel did the seminal

    work … ! (Thanks @Mark_Baker)
  28. FOSDEM 2014 | A Co-op game … but we diverged

    in virtually every aspect by now ! (Thanks @ericgazoni) (You’re welcome)
  29. FOSDEM 2014 | A Co-op game Neurodebian team made it

    fly ! (Thanks @yarikoptic and @peircej)
  30. FOSDEM 2014 | A Co-op game (kidding, we do not

    support VBA, we just stopped throwing it away)
  31. FOSDEM 2014 | A Co-op game • Pandas • PsycoPy

    • CubicWeb (timeseries cube) • Tablib • Khan Academy • Python (x,y) • …
  32. FOSDEM 2014 | Cool story, now what’s next ? Target

    for 1.9: bug fixes ! Expected in April 2014
  33. FOSDEM 2014 | Cool story, now what’s next ? 2.0

    is coming with backward-incompatible changes ! Expected in 2014
  34. FOSDEM 2014 | Cool story, now what’s next ? Used

    by many people, time to communicate changes responsively
  35. FOSDEM 2014 | Cool story, now what’s next ? lxml

    as default backend ? ! Benchmarks will tell
  36. FOSDEM 2014 | Cool story, now what’s next ? Validate

    all XML before it hits Excel (and displays embarrassing error messages)
  37. FOSDEM 2014 | Cool story, now what’s next ? 60+

    open bugs (want to step into open source development ?)