Advanced mode – openpyxl for Excel manipulation
In this section, we will look at the openpyxl
package, which allows for a more nuanced interaction with Excel when writing data.
Creating a new workbook
To start working with Excel sheets in Python, we need to create a new workbook. openpyxl
provides an intuitive API to create, modify, and save Excel workbooks. Here’s an example code snippet that demonstrates creating a new workbook:
import openpyxl # Create a new workbook workbook = openpyxl.Workbook()
Once again, the preceding code snippet doesn’t return anything but it does have a side effect – it creates the workbook:
Figure 2.4 – Creating a workbook with openpyxl
Adding sheets to the workbook
Once we have a workbook, we can add sheets to it. Adding sheets allows us to organize data into separate sections or categories. openpyxl
provides a simple method, create_sheet()
, to add sheets to a workbook. Let...