Python For Excel Felix Zumstein PDF Download
Python For Excel Felix Zumstein PDF Download
https://textbookfull.com/product/python-for-excel-felix-zumstein/
https://textbookfull.com/product/python-for-excel-a-modern-
environment-for-automation-and-data-analysis-1st-edition-felix-
zumstein/
https://textbookfull.com/product/advancing-into-analytics-from-
excel-to-python-and-r-1st-edition-mount-george/
https://textbookfull.com/product/statistics-for-managers-using-
microsoft-excel-levine/
Excel Crash Course For Engineers Eklas Hossain
https://textbookfull.com/product/excel-crash-course-for-
engineers-eklas-hossain/
https://textbookfull.com/product/analysis-and-implementation-of-
isogeometric-boundary-elements-for-electromagnetism-felix-wolf/
https://textbookfull.com/product/excel-vba-programming-for-
dummies-5th-edition-michael-alexander/
https://textbookfull.com/product/financial-modeling-in-excel-for-
dummies-2nd-edition-fairhurst/
https://textbookfull.com/product/matplotlib-for-python-
developers-effective-techniques-for-data-visualization-with-
python-2nd-edition-yim/
Python for Excel
A Modern Environment for Automation and Data
Analysis
With Early Release ebooks, you get books in their earliest form—
the author’s raw and unedited content as they write—so you can
take advantage of these technologies long before the official
release of these titles.
Felix Zumstein
Python for Excel
by Felix Zumstein
Copyright © 2021 Zoomer Analytics LLC. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles
(http://oreilly.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Constant width
Used for program listings, as well as within paragraphs to refer to
program elements such as variable or function names, filenames,
file extensions, databases, data types, environment variables,
statements, and keywords.
TIP
This element signifies a tip or suggestion.
NOTE
This element signifies a general note.
WARNING
This element indicates a warning or caution.
C:\Users\username\python-for-excel
C:\...\Downloads\python-for-excel-1st-edition\python-for-
excel-1st-edition
Copying the contents of this folder into one you create under
C:\Users\username\python-for-excel might make it easier
for you to follow along. The same remarks are true for macOS, i.e.
copy the files to /Users/username/python-for-excel.
If you have a technical question or a problem using the code
examples, please send email to bookquestions@oreilly.com.
This book is here to help you get your job done. In general, if
example code is offered with this book, you may use it in your
programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the
code. For example, writing a program that uses several chunks of
code from this book does not require permission. Selling or
distributing examples from O’Reilly books does require permission.
Answering a question by citing this book and quoting example code
does not require permission. Incorporating a significant amount of
example code from this book into your product’s documentation
does require permission.
We appreciate, but generally do not require, attribution. An
attribution usually includes the title, author, publisher, and ISBN. For
example: “Python for Excel by Felix Zumstein (O’Reilly). Copyright
2021 Zoomer Analytics LLC, 978-1-492-08100-5.”
If you feel your use of code examples falls outside fair use or the
permission given above, feel free to contact us at
permissions@oreilly.com.
NOTE
For more than 40 years, O’Reilly Media has provided technology and
business training, knowledge, and insight to help companies succeed.
Sebastopol, CA 95472
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples,
and any additional information. You can access this page at
https://oreil.ly/py4excel.
Email bookquestions@oreilly.com to comment or ask technical
questions about this book.
For more information about our books, courses, conferences, and
news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
As a first-time author, I am incredibly grateful for the help I got from
so many people along the way—they made this journey a lot easier
for me!
At O’Reilly, I would like to thank my editor, Melissa Potter, who did a
great job in keeping me motivated and on schedule and who helped
me to bring this book into a readable form. I’d also like to thank
Michelle Smith who worked with me on the initial book proposal and
Daniel Elfanbaum who wasn’t getting tired to answer my technical
questions.
A big thank you goes to all my colleagues, friends and clients who
invested many hours in reading the earliest form of my drafts. Their
feedback was crucial to make the book easier to understand and
some of the case studies are inspired by their real-world Excel
problems that they shared with me. My thanks go to Adam
Rodriguez, Mano Beeslar, Simon Schiegg, Rui Da Costa, Jürg Nager,
and Christophe de Montrichard.
I also got helpful feedback from readers of the Early Release version
that was published on the O’Reilly online learning platform. Thank
you Felipe Maion, Ray Doue, Kolyu Minevski, and David Ruggles!
I was very lucky that the book got reviewed by highly qualified tech
reviewers and I really appreciate the hard work they put in under a
lot of time pressure. Thanks for all your help, Jordan Goldmeier,
George Mount, Andreas Clenow, Werner Broennimann, and Eric
Moreira!
Special thanks go to Björn Stiel, who wasn’t just a tech reviewer but
from whom I also learned many of the things I am writing about in
this book. I’ve enjoyed working with you over the last couple of
years!
Last but not least, I’d like to extend my gratitude to Eric Reynolds
who merged his ExcelPython project into the xlwings code base in
2016 and who completely rewrote my horrible xlwings API from the
early days, turning it into the Excel-Python package with—in my
eyes—the most beautiful API as of today.
1 Microsoft has started to use the term custom functions instead of UDFs. In
this book, I keep calling them UDFs though.
Part I. Introduction to Python
Chapter 1. Why Python for
Excel?
Separation of concerns
One of the most important design principles in programming is
separation of concerns, sometimes also referred to as modularity. It
means that a related set of functionality should be taken care of by
an independent part of the program so it can be easily replaced
without affecting the rest of the application. At the highest level, an
application is often divided into the following layers:5
Presentation layer
Business layer
Data layer
To explain these layers, consider a simple currency converter as
shown in Figure 1-1. You’ll find the Excel file in the xl folder of the
companion repository.
Figure 1-1. currency_converter.xlsx
Business layer
This layer takes care of the application-specific logic: cell D4
defines how the amount is converted into USD. The formula =A4
* VLOOKUP(B4, F4:G11, 2, FALSE) translates to Amount
x Exchange rate.
Data layer
As the name suggests, this layer takes care of accessing the
data: the VLOOKUP part of cell D4 is doing this job.
The data layer accesses the data from the exchange rates table that
starts in cell F3 and that acts as the database of this little
application. If you paid close attention, you probably noticed that
cell D4 appears in all three layers: this simple application mixes the
presentation, business and data layers in a single cell. This isn’t
necessarily an issue for this simple currency converter, but often,
what starts off as a small Excel file turns soon enough into a much
bigger application. How can this situation be improved? Most
professional Excel developer resources advise you to use a separate
sheet for each layer, in Excel’s terminology usually called inputs,
calculations and outputs. Often, this is combined with defining a
certain color code for each layer, e.g. a blue background for all input
cells. In Chapter 11, we will build a real application based on these
layers: Excel will be the presentation layer, while the business and
data layers are moved to Python where it’s much easier to structure
your code properly.
Now that you know what separation of concerns means, let’s find
out what the DRY principle is!
DRY principle
The Pragmatic Programmer by Hunt and Thomas (Pearson
Education) popularized the DRY principle: don’t repeat yourself. No
duplicated code means fewer lines of code and fewer errors which
makes the code easier to maintain. If your business logic sits in your
cell formulas it’s practically impossible to apply the DRY principle as
there is no mechanism that allows you to reuse it in another
workbook. This, unfortunately, means that a common way to start a
new Excel project is to copy the workbook from the previous project
or from a template.
If you write VBA, the most common piece of reusable code is a
function. A function gives you access to the same code block from
multiple macros, for example. If you have multiple functions that you
use all the time, you might want to share them between workbooks.
The standard instrument to share VBA code across workbooks are
add-ins, but VBA add-ins lack a robust way of distributing and
updating them. While Microsoft has introduced an Excel internal
add-in store to solve that issue, this only works with JavaScript-
based add-ins, so it’s not an option for VBA coders. This means that
it is still very common to use the copy/paste approach with VBA:
let’s assume that you need a cubic spline function in Excel. The cubic
spline function is a way to interpolate a curve based on a few given
points in a coordinate system and is often used by fixed income
traders to derive an interest rate curve overall maturities based on a
few known maturity/interest rate combinations. If you search for
“Cubic Spline Excel” on the internet, it won’t take too long until you
have a page of VBA code that does what you want. The issue with
this is that most commonly, these functions were written by a single
person with probably good intentions but without formal
documentation or testing. Maybe they work for the majority of
inputs but what about some uncommon edge cases? If you are
trading a multimillion fixed income portfolio, you want to have
something you know you can trust. At least that is what you will
hear from your internal auditors when they find out where the code
is coming from.
Python makes it easy to distribute code by using a package manager
as we will see in the last section of this chapter. Before we get there,
however, let’s continue with testing, one of the cornerstones of solid
software development.
Testing
When you tell an Excel developer to test their workbooks, they will
most likely perform a few random checks: click a button and see if
the macro still does what it is supposed to do or change a few inputs
and check if the output looks reasonable. This is, however, a risky
strategy: Excel makes it very easy to introduce errors that are hard
to spot. For example, you can overwrite a formula with a hardcoded
value. Or you forget to adjust a formula in a hidden column.
When you tell a professional software developer to test their code,
they will write unit tests. As the name suggests, it’s a mechanism to
test individual components of your program. For example, unit tests
make sure that a single function of a program works properly. Most
programming languages offer a way to run unit tests automatically.
Running automated tests will increase the reliability of your
codebase dramatically and make reasonably sure that you won’t
break anything that currently works when you edit your code.
If you look at the currency conversion tool in Figure 1-1, you could
write a test that checks if the formula in cell D4 correctly returns
USD 105 with the following inputs: 100 EUR as amount and 1.05 as
the EURUSD exchange rate. Why does this help? Assume that you
accidentally delete cell D4 with the conversion formula and have to
rewrite it: instead of multiplying the amount with the exchange rate
you divide by it—after all, working with currencies can be confusing.
When you run the above test, you will get a test failure as 100 EUR /
1.05 will not result in 105 USD anymore as the test expects. Like
this, you can detect and fix the formula before you hand the
spreadsheet over to your users.
Pretty much all traditional programming languages offer one or more
test frameworks to write unit tests without much effort—but not
Excel. Fortunately, the concept of unit tests is simple enough and by
connecting Excel with Python you get access to Python’s powerful
unit testing frameworks. While a more in-depth presentation of unit
tests is beyond the scope of this book, I invite you to have a look at
my blog post where I walk you through the topic with practical
examples.
Unit tests are often set up to run automatically when you commit
your code to your version control system. The next section explains
what version control systems are and why they are hard to use with
Excel files.
Version control
Another characteristic of professional programmers is that they use a
system for version control or source control. A version control
system (VCS) tracks changes in your source code over time, allowing
you to see who changed what, when and why and allows you to
revert to old versions at any point in time. The most popular version
control system nowadays is Git. It was originally created to manage
the Linux source code and since then has conquered the
programming world—even Microsoft adopted Git in 2017 to manage
the Windows source code. In the Excel world, by contrast, the by far
most popular version control system comes in the form of a folder
where files are archived like this:
currency_converter_v1.xlsx
currency_converter_v2_2020_04_21.xlsx
currency_converter_final_edits_Bob.xlsx
currency_converter_final_final.xlsx
Another Random Document on
Scribd Without Any Related Topics
The following examples serve to show that the same extended types
of line which were found to be the commonest in Layamon’s Brut
(cp. p. 77) recur as the most usual types also in this poem:
A + C: Álle bèon he blíþe | þat tò my sóng lýþe! 1–2.
A + A: A sáng ihc schàl ȝou sínge | of Múrrȳ̀ þè kínge. 2–3.
A + A: He fónd bì þe strónde, | aríued òn his lónde, 35–6.
B + C: Àll þe dáy and àl þe nī́ȝt, | tìl hit spráng dái lìȝt. 123–4.
B + B: Fàirer nis nón þàne he wás, | hè was bríȝt sò þe glás. 13–14.
For-sóþ flípers,
And alle fáls fláters
I réde, sone, þou flé;
For þen sálle na gode mán,
Þat any góde lare cán,
Þár-fore blame þé.
In the same stanza The Feest (Hazlitt, Remains, iii. 93) is written.
Still more frequently such lines were used for extended tail-rhyme-
stanzas rhyming on the scheme a a a b c c c b d d d b e e e b, as
e.g. in a poem, The Enemies of Mankind, of the beginning of the
fourteenth century, published by Kölbing (Engl. Studien, ix. 440 ff.).
The first stanza runs as follows:
Þe sìker sóþe who so séys,
Wiþ dìol dréye we our dáys
And wàlk máni wil wáys
As wándrand wíȝtes.
Al our gámes ous agás,
So mani ténes on tás
Þurch fónding of fele fás,
Þat fást wiþ ous fíȝtes.
Our flèsche is fóuled wiþ þe fénd;
Þer we fínde a fals frénde:
Þei þai héuen vp her hénde,
Þai no hóld nouȝt her híȝtes.
Þis er þré, þat er þrá,
Ȝete þe férþ is our fá,
Dèþ, þat dérieþ ous swá
And díolely ous díȝtes.
Here, again, the difference between the lines on the pattern of the
first hemistich of the long line, which form the body of the stanza (a
a a, b b b, c c c, d d d), and those on the pattern of the second
hemistich used as tail-rhyme lines (b, b, b, b) is plainly recognizable.
The same is the case in other poems written in this form of stanza,
as e.g. in the Metrical Romances, Sir Perceval, Sir Degrevant
(Halliwell, Thornton Romances, Camden Society, 1844, pp. 1, 177)
and others; cf. Luick, Anglia, xii, pp. 440ff., and Paul’s Grundriss, ii a,
p. 1016. But in these later works, one of the latest of which probably
is the poem The Droichis Part of the Play, possibly by Dunbar (Laing,
ii. 37; Small, ii. 314; Schipper, 190), the two-beat lines are
frequently intermingled and blended with even-beat lines, which
from the beginning of the fifth stanza onward completely take the
place of the two-beat lines in the last-mentioned poem. Likewise in
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com