The business world uses Microsoft Office. Their spreadsheet software solution, Microsoft Excel, is especially popular. Excel is used to store tabular data, create reports, graph trends, and much more. Before diving into working with Excel with Python, let’s clarify some special terminology:
Spreadsheet or Workbook – The file itself (.xls or .xlsx).
Worksheet or Sheet – A single sheet of content within a Workbook. Spreadsheets can contain multiple Worksheets.
Column – A vertical line of data that is labeled with letters, starting with “A”.
Row – A horizontal line of data labeled with numbers, starting with 1.
Cell – A combination of Column and Row, like “A1”.
In this article, you will be using Python to work with Excel Spreadsheets. You will learn about the following:
Python Excel Packages
Getting Sheets from a Workbook
Reading Cell Data
Iterating Over Rows and Columns
Writing Excel Spreadsheets
Adding and Removing Sheets
Adding and Deleting Rows and Columns
Excel is used by most companies and universities. It can be used in many different ways and enhanced using Visual Basic for Applications (VBA). However, VBA is kind of clunky — which is why it’s good to learn how to use Excel with Python.
Python has a built-in library called json that you can use for creating, editing and parsing JSON. You can read all about this library here:
From Python’s point of view, this JSON is a nested Python dictionary. You will find that JSON is always translated into some kind of native Python data type. In this article, you will learn about the following:
Mistakes in your code are known as “bugs”. You will make mistakes. You will make many mistakes, and that’s totally fine. Most of the time, they will be simple mistakes such as typos. But since computers are very literal, even typos prevent your code from working as intended. So they need to be fixed. The process of fixing your mistakes in programming is known as debugging.
The Python programming language comes with its own built-in debugger called pdb. You can use pdb on the command line or import it as a module. The name, pdb, is short for “Python debugger”.
In this article, you will familiarize yourself with the basics of using pdb. Specifically, you will learn the following:
Starting pdb in the REPL
Starting pdb on the Command Line
Stepping Through Code
Adding Breakpoints in pdb
Creating a Breakpoint with set_trace()
Using the built-in breakpoint() Function
While pdb is handy, most Python editors have debuggers with more features. You will find the debugger in PyCharm or WingIDE to have many more features, such as auto-complete, syntax highlighting, and a graphical call stack.
A call stack is what your debugger will use to keep track of function and method calls. When possible, you should use the debugger that is included with your Python IDE as it tends to be a little easier to understand.
However, there are times where you may not have your Python IDE, for example when you are debugging remotely on a server. It is those times when you will find pdb to be especially helpful.
There are times when you are writing an application and you need to run another application. For example, you may need to open Microsoft Notepad on Windows for some reason. Or if you are on Linux, you might want to run grep. Python has support for launching external applications via the subprocess module.
The subprocess module has been a part of Python since Python 2.4. Before that you needed to use the os module. You will find that the subprocess module is quite capable and straightforward to use.
Application developers are always working with files. You create them whenever you write a new script or application. You write reports in Microsoft Word, you save emails or download books or music. Files are everywhere. Your web browser downloads lots of little files to make your browsing experience faster.
When you write programs, you have to interact with pre-existing files or write out files yourself. Python provides a nice, built-in function called open() that can help you with these tasks.