- [Instructor] Using XlsxWriter,
you can also programmatically set
the document properties of a workbook,
and these properties contain information about the workbook
such as the title, the author,
descriptive keywords, and so on.
You can see these properties when you're in Excel
by going to the Properties Viewer,
so let's run our example code as it currently is
before we make any changes,
just to create an empty workbook
so we can see what we're talking about.
So I'm going to run this
and you can see that properties.xlsx has been created,
so let's go ahead and open that in Excel.
So the way you do this on the Mac
is by going to the File menu
and choosing properties about the the workbook.
Here on Windows,
I'm going to go to the File tab and click on Info,
and then right here where it says Properties,
I'm going to click on this and choose Advanced Properties.
And you can see that here in the Summary tab,
there's title, subject, author, manager, company, so on.
There's a hyperlink base property,
and then over here in the Custom tab,
there's a whole bunch of other properties that you can add.
Now, currently these are blank,
so we're going to set these in our workbook, alright?
Let's cancel out of this and close our workbook
and go back to our code.
So these properties don't have any effect
on your workbook whatsoever,
but they can be read and used by external applications
for a variety of purposes,
including custom workflows or search indexing.
The workbook class defines two methods you can use
to set properties on the Excel files.
The Set Properties function
takes a dictionary object with predefined values
for the standard document properties,
and the set custom property function
can be used to store property values
that are not within the standard set
covered by the more general set properties function.
In this example, we're going to use both.
Alright, so let's update our code
to define some standard properties,
and I'm going to define those as a dictionary.
So I'll add a title
and that's going to be the Document Properties Example.
And let's see the subject.
We'll just say,
"Shows how to use document properties in XlsxWriter."
The author.
Well, I guess that's going to be me.
The manager, let's set that to Colonel Monogram.
If you know, you know.
If you don't, go ahead and do a search.
For the category,
I'll just make that the Example Spreadsheets category.
Let's specify some keywords.
Let's see, Properties, Sample, XlsxWriter.
That should be good.
And let's add some comments.
Created using XlsxWriter as a LinkedIn Learning example.
Okay, that should do it.
Let's go ahead and write these into the workbook,
and the way that we do that is by using the workbook
and calling set properties with our dictionary.
And let's also set a couple of custom properties.
So on the workbook, I'm going to call set_custom_property.
Let's set the checked by property,
which is one of the ones that was already there,
and that'll be checked by Perry P.
And now let's set a custom property
that doesn't already exist,
and I'll make that the Approved property
and I'll set that to True.
Okay, so let's save our code.
So now we're using both the standard
and non-standard properties.
Let's run our code again.
Alright, seems to have worked.
Let's jump over to Excel.
Go to the Info tab, choose the Advanced Properties,
and now we can see that we filled out
most of these properties, right?
Here's the title, the subject, the author, right?
All of these properties that we filled in.
So these are all the standard ones, okay?
And you can see that these are all the values
that we set in the code.
And if we go to the custom tab,
we could see that the custom properties have also been set.
So the checked by value is Perry P.
Approved is yes,
and we can see the types over here, alright?
So these properties can be used
by both Office and external applications,
so if you have a custom workflow
that needs to use these properties,
you can create them programmatically using XlsxWriter.