Python: Add Document Properties in Excel

2024-01-11 01:08:14 Written by  support iceblue
Rate this item
(0 votes)

In Excel, document properties refer to the metadata or information associated with an Excel file. These properties provide details about the workbook itself, such as author, title, subject, keywords, and other descriptive information. Document properties are useful for organizing and categorizing Excel files, making it easier to search, sort, and manage a collection of workbooks. In this article, you will learn how to add document properties in Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Add Built-in Document Properties in Excel in Python

Built-in document properties are basic information about a document such as title, subject, author, category, etc. The names of these properties are predefined that cannot be edited, but Spire.XLS for Python allows you to set specific values for these properties. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Get the built-in document properties of the document using Workbook.DocumentProperties property.
  • Set specific document properties such as title, author, keywords and comments using the properties of BuiltInDocumentProperties class.
  • Save the result document using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "sample.xlsx"
outputFile = "ExcelProperties.xlsx"

# Create a Workbook object
workbook = Workbook()

# Load a sample Excel document
workbook.LoadFromFile(inputFile)

# Set built-in document properties for the Excel workbook
workbook.DocumentProperties.Author = "Jamie"
workbook.DocumentProperties.Title = "Add Built-in Document Properties in Excel"
workbook.DocumentProperties.Subject = "Processing Excel with Python"
workbook.DocumentProperties.Keywords = "Python, Excel, Document Properties, Demo"
workbook.DocumentProperties.Category = "Excel Demo"
workbook.DocumentProperties.Company = "E-iceblue"
workbook.DocumentProperties.Comments = "Document properties are details about a file that describe or identify it."

# Save the result document
workbook.SaveToFile(outputFile, FileFormat.Version2016)
workbook.Dispose()

Python: Add Document Properties in Excel

Add Custom Document Properties in Excel in Python

Custom document properties are additional properties that you can define for an Excel document. With Spire.XLS for Python, you can add custom properties with specified names and values through the ICustomDocumentProperties.Add() method. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Get the custom document properties of the document using Workbook.CustomDocumentProperties property.
  • Add custom document properties with different data types to the document using ICustomDocumentProperties.Add() method.
  • Save the result document using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "sample.xlsx"
outputFile = "ExcelCustomProperties.xlsx"

# Create a Workbook object
workbook = Workbook()

# Load a sample Excel document
workbook.LoadFromFile(inputFile)

# Add a custom property to make the document as final
workbook.CustomDocumentProperties.Add("_MarkAsFinal", True)

# Add other custom properties to the document
workbook.CustomDocumentProperties.Add("The Editor", "E-iceblue")
workbook.CustomDocumentProperties.Add("Phone number", 81705109)
workbook.CustomDocumentProperties.Add("Revision number", 7.12)
workbook.CustomDocumentProperties.Add("Revision date", DateTime.get_Now())

# Save the result document
workbook.SaveToFile(outputFile, FileFormat.Version2016)
workbook.Dispose()

Python: Add Document Properties in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:13