From the course: Advanced Hands-On Python: Working with Excel and Spreadsheet Data

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Creating a workbook

Creating a workbook

- [Instructor] Now that we've seen how to load and explore an existing workbook, let's try our hand at creating one from scratch. So let's go ahead and open up the code in create_workbook.py. And here in my code I've imported the openpyxl library as well as some other imports we're going to be using in a few moments. So to create a new workbook, I just need to use the constructor for the class. So I'm going to make a variable named wb and I'm going to set that to the result of calling the Workbook function. So that creates a new empty workbook with a default worksheet in it. So to write content into the workbook, we have to get access to a worksheet, and one way to do that is to get the active sheet. So I'll make a variable called sheet and I'll set that to wb.active. And then I'm going to name that sheet by setting the title property equal to First. All right, so now that we've created our first workbook and first worksheet, let's add some data. We can treat the worksheet as an…

Contents