In this series we’ll be using the openpyxl module to work on spreadsheets. In this part we’ll install the module and create a simple workbook. I will use the terms spreadsheet and workbook interchangeably throughout the series.
Spreadsheets are used a lot in all domains of science to manipulate large datasets easily and without prior technical background necessary to get started.
If you don’t have the openpyxl module on your machine yet, go to the terminal and install it:
pip install openpyxl
With the module installed let’s make sure everything works. Let’s create a spreadsheet. First we need
to import the Workbook class from the openpyxl module:
xxx
from openpyxl import Workbook
Now we can create an object of the Workbook class, which represents the spreadsheet:
workbook = Workbook()
Now let’s assign the active worksheet to a variable. Each spreadsheet may consist of one or more sheets:
sheet = workbook.active
We set the content of the cells using the coordinates of the columns and rows:
sheet['A1'] = 'first cell'
sheet['C2'] = 'It works!'
Finally we have to save the file:
workbook.save(filename = 'first.xlsx')
This creates a spreadsheet file in your current directory. You can now go there and open it by double-clicking on the icon for example.
That’s it. We’ll have a look at all the steps described above in much more detail in later parts of the series.