- [Instructor] Let's start with a simple example
by using XlsxWriter
to create a workbook and add some content to it.
So we're going to open up our sample file xlsx_create.
Alright, so here in my example file,
I have imported the xlsxwriter module along with date time,
which I'm going to need later.
So first let's create the workbook.
And the way that we do this is by
creating a workbook variable.
And then I'm going to use xlsxwriter
and I'm going to use the workbook class
and I'm going to give it a name.
I'm going to call it XlsxBasics.xlsx.
Alright, and then let's add a worksheet.
So on the workbook I'm going to call add worksheet,
and I'm going to call it Test Sheet.
So once the workbook is created, let's add some content,
and we can do this using letter row notation or by using row
and column indexes.
So first, let's try the letter and numbers.
So I'll have worksheet
and I'll call the write function to add content.
And in A1 I'm going to write "Hello World."
And then I'll do the same thing.
But in this case, I'm going to use the worksheet.write function
and I'm going to specify row 1,
column 0, and then the same thing.
Now the write function does what you'd expect.
It writes content into a cell.
The right function tries
to determine the cell type based
upon the data that's being written.
So in this case, I'm writing strings,
but if I were writing numbers
or dates, then the right function would try to evaluate
what data type it was
and set the data type in the worksheet appropriately.
Now there are type specific right functions.
So if you know the data type that you want to be written,
then you can just use those functions.
So for example, I can use worksheet
and I can call write_number,
and I'll put that in row 2, column 0,
and I can use worksheet dot and I'll call write_boolean
and I'll put that in row 3.
And then let's do another example.
I'll call write_url,
and we'll put that in row 4
and we'll put a URL in there.
All right, and let's also
write a date_time object to the sheet.
So I'll create a date_time,
and that's going to be the datetime.datetime.
And I'm going to use the strptime function
to convert a date.
Let's do July 28th,
and we're going to convert that into a year,
oops, year dash month dash day format.
And then I'll specify a date format.
I'm going to use the add format function here,
and we'll learn about this later in the chapter.
So don't worry about this too much right now.
I'm going to specify a num_format,
and that is going to be short day
with the month and the four digit year.
And then I'll tell the worksheet to write, oops,
datetime, and I'll put that in row 5,
and we'll specify the datetime
and the date format to write.
All right?
You can also write multiple values at once,
using the write row and write column functions.
So let's give that a try.
I'll create a little values array
and I'll put some strings in here.
All right.
And then on the worksheet, let's call write_row
and we'll put that in A6.
Proving once again, you can use row column
or you can use letter number and we'll write values.
And then we'll do the same thing with writing the column,
and we'll put that in D1 and we'll write the same values.
Alright, and then finally, let's take advantage of some
of the nice features that XlsxWriter gives us.
Let's set the zoom level of the worksheet
because we can do that easily here.
So on worksheet, I'm going to call set_zoom,
and let's make that 200.
And then finally, let's call the close function,
which we'll save and close the workbook.
Alright, so we've got a bunch of code now that
creates a workbook, adds a worksheet,
puts some content in there.
We're using different writing functions
to write specific data types.
We're writing multiple values, setting the zoom factor,
and then closing the workbook, right?
Let's go ahead and run our code.
All right?
And when we run the code, we can see
that XlsxBasics has been created.
So let's hop on over to Excel
and take a look at our handiwork
and we can see that the output file has been created.
And here's all of our content.
So let's see, we've got data has been entered
into the various cells, we can see that sure enough,
the workbook's zoom level is 200%.
So here are the various different types of data
that we've written into the workbook,
and we've got our multiple values here, right?
So everything is looking pretty good.
So looks like we're off to a good start.