Python (359)
Python: Enable Track Changes, Accept or Reject Tracked Changes in Word
2024-04-07 01:31:37 Written by KoohjiTrack changes in Microsoft Word is a powerful feature that facilitates document collaboration and review processes. When track changes is enabled, any modifications made to the document, such as text additions or deletions, formatting changes, and comments, are visually highlighted. This makes it easier for document editors or collaborators to identify and review the changes made by themselves or others. In this article, we will explain how to enable track changes, as well as accept or reject the tracked changes in Word documents in Python using Spire.Doc for Python.
- Enable Track Changes in Word in Python
- Accept Tracked Changes in Word in Python
- Reject Tracked Changes in Word in Python
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Enable Track Changes in Word in Python
Spire.Doc for Python offers the Document.TrackChanges property to enable the track changes mode for a Word document. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Enable the track changes mode for the document by setting the Document.TrackChanges property to True.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an object of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Enable the track changes mode for the document
doc.TrackChanges = True
# Save the result document
doc.SaveToFile("EnableTrackChanges.docx", FileFormat.Docx2016)
doc.Close()

Accept Tracked Changes in Word in Python
Accepting tracked changes allows you to incorporate the suggested modifications permanently into the document. By using the Document.AcceptChanges() method provided by Spire.Doc for Python, you can easily accept all tracked changes in a Word document. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Check if the document has tracked changes using Document.HasChanges property.
- Accept the tracked changes in the document using Document.AcceptChanges() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an object of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Check if the document has tracked changes
if(doc.HasChanges):
# Accept the tracked changes in the document
doc.AcceptChanges()
# Save the result document
doc.SaveToFile("AcceptChanges.docx", FileFormat.Docx2016)
doc.Close()

Reject Tracked Changes in Word in Python
Sometimes, suggested modifications may not align with your vision or requirements for the document. In such cases, rejecting these changes becomes essential to ensure that the document accurately reflects your intended content and formatting choices.
Spire.Doc for Python offers the Document.RejectChanges() method to reject the tracked changes in a Word document. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Check if the document has tracked changes using Document.HasChanges property.
- Reject the tracked changes in the document using Document.RejectChanges() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an object of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Check if the document has tracked changes
if(doc.HasChanges):
# Reject the tracked changes in the document
doc.RejectChanges()
# Save the result document
doc.SaveToFile("RejectChanges.docx", FileFormat.Docx2016)
doc.Close()

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.
PDF files often contain bookmarks, which are clickable links that make navigating lengthy documents easier. Extracting these bookmarks can be beneficial for creating an outline of the document, analyzing document structure, or identifying key topics or sections. In this article, you will learn how to extract PDF bookmarks with Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Extract Bookmarks from PDF Using Python
With Spire.PDF for Python, you can create custom methods GetBookmarks() and GetChildBookmark() to get the title and text styles of both parent and child bookmarks in a PDF file, then export them to a TXT file. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get bookmarks collection in the PDF file using PdfDocument.Bookmarks property.
- Call custom methods GetBookmarks() and GetChildBookmark() to get the text content and text style of parent and child bookmarks.
- Export the extracted PDF bookmarks to a TXT file.
- Python
from spire.pdf.common import *
from spire.pdf import *
inputFile = "AnnualReport.pdf"
result = "GetPdfBookmarks.txt"
def GetChildBookmark(parentBookmark, content):
if parentBookmark.Count > 0:
# Iterate through each child bookmark in the parent bookmarks
for i in range(parentBookmark.Count):
childBookmark = parentBookmark.get_Item(i)
# Get the title
content.append(childBookmark.Title)
# Get the text style
textStyle = str(childBookmark.DisplayStyle)
content.append(textStyle)
cldBk = PdfBookmarkCollection(childBookmark)
GetChildBookmark(cldBk, content)
def GetBookmarks(bookmarks, result):
# Create an object of StringBuilder
content = []
# Get PDF bookmarks information
if bookmarks.Count > 0:
content.append("Pdf bookmarks:")
# Iterate through each parent bookmark
for i in range(bookmarks.Count):
parentBookmark = bookmarks.get_Item(i)
# Get the title
content.append(parentBookmark.Title)
# Get the text style
textStyle = str(parentBookmark.DisplayStyle)
content.append(textStyle)
cldBk = PdfBookmarkCollection(parentBookmark)
GetChildBookmark(cldBk, content)
# Save to a TXT file
with open(result, "w") as file:
file.write("\n".join(content))
# Create a PdfDocument instance
pdf = PdfDocument()
# Load a PDF file from disk.
pdf.LoadFromFile(inputFile)
# Get bookmarks collection of the PDF file
bookmarks = pdf.Bookmarks
# Get the contents of bookmarks and save them to a TXT file
GetBookmarks(bookmarks, result)
pdf.Close()

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.
Excel is a powerful tool for organizing and analyzing data, but the default black-and-white spreadsheet can make it difficult to interpret information at a glance. By adding fill colors, gradients, or patterns to specific cells, you can highlight important data, separate different categories of information, and make the spreadsheet more visually appealing. In this article, you will learn how to set a fill color, gradient or pattern in Excel cells using Spire.XLS for Python.
- Add Fill Color to Excel Cells with Python
- Add Gradient Fill to Excel Cells with Python
- Add Fill Pattern to Excel Cells with 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 Fill Color to Excel Cells with Python
The CellRange.Style.Color property provided by Spire.XLS for Python allows to add a solid background color to Excel cells. The following are detailed steps.
- Create a Workbook instance.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get a specified cell or cell range using Worksheet.Range[] property.
- Add a background color to the cells using CellRange.Style.Color property.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * inputFile = "Cost.xlsx" outputFile = "CellFillColor.xlsx" # Create a Workbook instance workbook = Workbook() # Load an Excel file from disk workbook.LoadFromFile(inputFile) # Get the first worksheet worksheet = workbook.Worksheets[0] # Set fill color for a cell or cell range worksheet.Range["A1:D1"].Style.Color = Color.get_Green() worksheet.Range["A5"].Style.Color = Color.get_Yellow() # Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2016) workbook.Dispose()

Add Gradient Fill to Excel Cells with Python
To apply gradient fill in Excel, you first need to set the cell fill pattern type to gradient, and then specify two colors and the shading style of the gradient fill. The following are detailed steps.
- Create a Workbook instance.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get the cell style of a specified cell or cell range using Worksheet.Range[].Style property.
- Get the interior cell style using CellStyle.Interior property.
- Set cell fill effect to gradient through ExcelInterior.FillPattern property.
- Set the background and foreground colors of the gradient fill using ExcelInterior.Gradient.BackColor and ExcelInterior.Gradient.ForeColor properties.
- Set the gradient shading style using ExcelInterior.Gradient.GradientStyle property.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * inputFile = "Cost.xlsx" outputFile = "CellGradientFill.xlsx" # Create a Workbook instance workbook = Workbook() # Load an Excel file from disk workbook.LoadFromFile(inputFile) # Get the first worksheet worksheet = workbook.Worksheets[0] # Get the cell style of a specified range cellStyle = worksheet.Range["A1:A12"].Style # Set cell fill pattern type to gradient cellStyle.Interior.FillPattern = ExcelPatternType.Gradient # Set the background and foreground colors of the gradient fill cellStyle.Interior.Gradient.BackColor = Color.get_SkyBlue() cellStyle.Interior.Gradient.ForeColor = Color.get_White() # Set the gradient shading style cellStyle.Interior.Gradient.GradientStyle = GradientStyleType.From_Center # Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2016) workbook.Dispose()

Add Fill Pattern to Excel Cells with Python
You can also add predefined patterns such as different styles of stripes, dots, and crosshatch to specific cells through the CellRange.Style.FillPattern property. The following are the detailed steps:
- Create a Workbook instance.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get a specified cell or cell range using Worksheet.Range[] property.
- Add a background pattern to the cells using CellRange.Style.FillPattern property.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * inputFile = "Cost.xlsx" outputFile = "CellFillPattern.xlsx" # Create a Workbook instance workbook = Workbook() # Load an Excel file from disk workbook.LoadFromFile(inputFile) # Get the first worksheet worksheet = workbook.Worksheets[0] #Set cell fill pattern for a cell or cell range worksheet.Range["A7:C12"].Style.FillPattern = ExcelPatternType.Percent125Gray worksheet.Range["D5"].Style.FillPattern = ExcelPatternType.ThinDiagonalStripe # Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2016) workbook.Dispose()

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.
Slide titles play a crucial role in PowerPoint presentations because they can assist the audience in quickly grasping the topics or key points of each slide. When working with PowerPoint documents, users often encounter the need to update or extract slide titles for various purposes. For example, they may need to modify titles to reflect new content or extract titles to perform tasks such as summarizing or analyzing presentation content. Knowing how to programmatically update or extract slide titles can greatly save time and effort, particularly when dealing with extensive presentations. In this article, we will demonstrate how to update and extract slide titles in PowerPoint PPTX or PPT documents in Python using Spire.Presentation for Python.
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Presentation
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows
Update Slide Titles in PowerPoint in Python
The title of a slide can be updated using the ISlide.Title property. The detailed steps are as follows.
- Create a Presentation instance.
- Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
- Get a specific slide of the document using Presentation.Slides[index] property.
- Update the title of the slide using ISlide.Title property.
- Save the result document using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import *
from spire.presentation import *
# Create a Presentation object
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("Water Of Life.pptx")
# Get the second slide
slide = ppt.Slides[1]
# Update the title of the second slide
slide.Title = "Updated Title"
# Save the result document
ppt.SaveToFile("UpdateSlideTitle.pptx", FileFormat.Pptx2016)
ppt.Dispose()

Extract All Slide Titles from PowerPoint in Python
To extract all slide titles from a PowerPoint document, you first need to iterate through all slides in the document and all shapes on each slide. Then identify shapes with placeholder types like Title, CenteredTitle, or Subtitle. After that retrieve the text content from the identified shapes using the IAutoShape.TextFrame.Text property. The detailed steps are as follows.
- Create a Presentation instance.
- Load a PowerPoint PPTX or PPT document using Presentation.LoadFromFile() method.
- Create a list to store the extracted titles.
- Iterate through all slides in the document.
- For each slide, iterate through all shapes on it.
- Identify shapes with placeholder types such as Title, CenteredTitle, or Subtitle.
- Typecast the identified shapes to IAutoShape object.
- Retrieve the text content of the identified shapes using the IAutoShape.TextFrame.Text property and append them to the list.
- Save the content of the list to a text file.
- Python
from spire.presentation.common import *
from spire.presentation import *
# Load a PowerPoint document
ppt = Presentation()
ppt.LoadFromFile("Water of Life.pptx")
# Create a list to store the extracted slide titles
titles = []
# Iterate through all slides in the document
for slide in ppt.Slides:
# Iterate through all shapes on each slide
for shape in slide.Shapes:
# Find the shapes with placeholder types such as Title, CenteredTitle, or Subtitle
if shape.Placeholder is not None and shape.Placeholder.Type in [PlaceholderType.Title, PlaceholderType.CenteredTitle, PlaceholderType.Subtitle]:
# Typecast the shape to IautoShape object
auto_shape = shape if isinstance(shape, IAutoShape) else None
if auto_shape is not None:
# Add the text of the shape to the titles list
titles.append(auto_shape.TextFrame.Text)
# Save the extracted slide titles to a text file
with open("AllTitles.txt", "w") as file:
file.write("Extracted titles:\n")
file.write("\n".join(titles))
ppt.Dispose()

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.
In Microsoft Excel, freezing panes is a practical feature that allows you to lock specific rows or columns in place while navigating through a spreadsheet. When you freeze panes, the selected rows or columns remain visible on the screen, even as you scroll through the rest of the worksheet. This feature proves especially useful when dealing with large datasets where headers, titles, or other important reference points must stay visible to provide context.
Unfreezing panes, on the other hand, allows you to release the locked rows or columns. It is beneficial when you no longer need certain sections to remain fixed and want to regain the ability to navigate the entire spreadsheet without any restrictions. In this article, we will demonstrate how to freeze and unfreeze panes 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 commands.
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
Freeze Panes in Excel in Python
Rows and columns can be frozen by using the Worksheet.FreezePanes() method. This method takes two parameters - rowIndex (1-based) and columnIndex (1-based). The first parameter represents the index of the row above which all rows will be frozen. The second parameter represents the index of the column to the left of which all columns will be frozen.
The following steps show you how to freeze specific rows and/or columns in Excel using Spire.XLS for Python.
- Create an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet using Workbook.Worksheets[index] property.
- Freeze specific rows and/or columns in the worksheet using Worksheet.FreezePanes(rowIndex, columnIndex) method.
- Save the result Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Specify the paths for the input and output Excel files inputFile = "Sample.xlsx" outputFile = "FreezePanes.xlsx" # Create a Workbook instance workbook = Workbook() # Load an Excel file workbook.LoadFromFile(inputFile) # Get the first worksheet in the file sheet = workbook.Worksheets[0] # Freeze specific rows and/or columns # Freeze the top row sheet.FreezePanes(2, 1) # Or freeze the first column # sheet.FreezePanes(1, 2) # Or freeze the top row and the first column # sheet.FreezePanes(2, 2) # Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2013) workbook.Dispose()

Unfreeze Panes in Excel in Python
To unfreeze rows and columns in Excel, use the Worksheet.RemovePanes() method. The detailed steps are as follows.
- Create an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet using Workbook.Worksheets[index] property.
- Unfreeze rows and columns in the worksheet using Worksheet.RemovePanes() method.
- Save the result Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Specify the paths for the input and output Excel files inputFile = " FreezePanes.xlsx" outputFile = "UnfreezePanes.xlsx" # Create a Workbook instance workbook = Workbook() # Load an Excel file workbook.LoadFromFile(inputFile) # Get the first worksheet in the file sheet = workbook.Worksheets[0] # Unfreeze rows and/or columns in the worksheet sheet.RemovePanes() # Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2013) workbook.Dispose()
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.
Efficiently managing Word documents often requires the task of splitting them into smaller sections. However, manually performing this task can be time-consuming and labor-intensive. Fortunately, Spire.Doc for Python provides a convenient and efficient way to programmatically segment Word documents, helping users to extract specific parts of a document, split lengthy documents into smaller chunks, and streamline data extraction. This article demonstrates how to use Spire.Doc for Python to split a Word document into multiple documents in Python.
The splitting of a Word document is typically done by page breaks and section breaks due to the dynamic nature of document content. Therefore, this article focuses on the following two parts:
- Split a Word Document by Page Breaks with Python
- Split a Word Document by Section Breaks with Python
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to: How to Install Spire.Doc for Python on Windows
Split a Word Document by Page Breaks with Python
Page breaks allow for the forced pagination of a document, thereby achieving a fixed division of content. By using page breaks as divisions, we can split a Word document into smaller content-related documents. The detailed steps for splitting a Word document by page breaks are as follows:
- Create an instance of Document class and load a Word document using Document.LoadFromFile() method.
- Create a new document, add a section to it using Document.AddSection() method.
- Iterate through all body child objects in each section in the original document and check if the child object is a paragraph or a table.
- If the child object is a table, add it to the section in the new document using Section.Body.ChildObjects.Add() method.
- If the child object is a paragraph, add the paragraph object to the section in the new document. Then, iterate through all child objects of the paragraph and check if a child object is a page break.
- If the child object in the paragraph is a page break, get its index using Paragraph.ChildObjects.IndexOf() method and remove it from the paragraph by its index.
- Save the new document using Document.SaveToFile() method and repeat the above process.
- Python
from spire.doc import *
from spire.doc.common import *
inputFile = "Sample.docx"
outputFolder = "output/SplitDocument/"
# Create an instance of Document
original = Document()
# Load a Word document
original.LoadFromFile(inputFile)
# Create a new word document and add a section to it
newWord = Document()
section = newWord.AddSection()
original.CloneDefaultStyleTo(newWord)
original.CloneThemesTo(newWord)
original.CloneCompatibilityTo(newWord)
index = 0
# Iterate through all sections of original document
for m in range(original.Sections.Count):
sec = original.Sections.get_Item(m)
# Iterate through all body child objects of each section
for k in range(sec.Body.ChildObjects.Count):
obj = sec.Body.ChildObjects.get_Item(k)
if isinstance(obj, Paragraph):
para = obj if isinstance(obj, Paragraph) else None
sec.CloneSectionPropertiesTo(section)
# Add paragraph object in original section into section of new document
section.Body.ChildObjects.Add(para.Clone())
for j in range(para.ChildObjects.Count):
parobj = para.ChildObjects.get_Item(j)
if isinstance(parobj, Break) and ( parobj if isinstance(parobj, Break) else None).BreakType == BreakType.PageBreak:
# Get the index of page break in paragraph
i = para.ChildObjects.IndexOf(parobj)
# Remove the page break from its paragraph
section.Body.LastParagraph.ChildObjects.RemoveAt(i)
# Save the new document
resultF = outputFolder
resultF += "SplitByPageBreak-{0}.docx".format(index)
newWord.SaveToFile(resultF, FileFormat.Docx)
index += 1
# Create a new document and add a section
newWord = Document()
section = newWord.AddSection()
original.CloneDefaultStyleTo(newWord)
original.CloneThemesTo(newWord)
original.CloneCompatibilityTo(newWord)
sec.CloneSectionPropertiesTo(section)
# Add paragraph object in original section into section of new document
section.Body.ChildObjects.Add(para.Clone())
if section.Paragraphs[0].ChildObjects.Count == 0:
# Remove the first blank paragraph
section.Body.ChildObjects.RemoveAt(0)
else:
# Remove the child objects before the page break
while i >= 0:
section.Paragraphs[0].ChildObjects.RemoveAt(i)
i -= 1
if isinstance(obj, Table):
# Add table object in original section into section of new document
section.Body.ChildObjects.Add(obj.Clone())
# Save the document
result = outputFolder+"SplitByPageBreak-{0}.docx".format(index)
newWord.SaveToFile(result, FileFormat.Docx2013)
newWord.Close()

Split a Word Document by Section Breaks with Python
Sections divide a Word document into different logical parts and allow for independent formatting for each section. By splitting a Word document into sections, we can obtain multiple documents with relatively independent content and formatting. The detailed steps for splitting a Word document by section breaks are as follows:
- Create an instance of Document class and load a Word document using Document.LoadFromFile() method.
- Iterate through each section in the document.
- Get a section using Document.Sections.get_Item() method.
- Create a new Word document and copy the section in the original document to the new document using Document.Sections.Add() method.
- Save the new document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")
# Iterate through all sections
for i in range(document.Sections.Count):
section = document.Sections.get_Item(i)
result = "output/SplitDocument/" + "SplitBySectionBreak_{0}.docx".format(i+1)
# Create a new Word document
newWord = Document()
# Add the section to the new document
newWord.Sections.Add(section.Clone())
#Save the new document
newWord.SaveToFile(result)
newWord.Close()

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.
Excel files often contain sensitive and confidential information, such as financial data, personal information, trade secrets, or proprietary formulas. When sharing these files over the internet or between organizations, there might be a risk of data leaks, theft, or unauthorized modifications. To address this concern, Excel provides a comprehensive set of protection features, such as password-protecting workbooks, restricting editing on worksheets, and locking cells, which enable users to establish multiple layers of security to control data access and maintain the integrity of their Excel files. In this article, you will learn how to protect and unprotect Excel workbooks and worksheets in Python using Spire.XLS for Python.
- Password Protect an Entire Workbook
- Protect a Worksheet with a Specific Protection Type
- Allow Users to Edit Ranges in a Protected Worksheet
- Unprotect a Password Protected Worksheet
- Remove or Reset the Password of an Encrypted Workbook
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 commands.
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
Password Protect an Entire Workbook in Python
By encrypting an Excel document with a password, you ensure that the data within the document remains secure and inaccessible to unauthorized individuals. The following are the steps to password-protect a workbook using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Protect the workbook with a password using Workbook.Protect() method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook
workbook.LoadFromFile("Sample.xlsx")
# Protect the workbook with a password
workbook.Protect("psd-123")
# Save the workbook to another Excel file
workbook.SaveToFile("Encrypted.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Protect a Worksheet with a Specific Protection Type in Python
If you want to authorize others to view your Excel document while limiting the types of changes they can make to a worksheet, you can protect the worksheet with a specific protection type. The table below lists a variety of pre-defined protection types under the SheetProtectionType enumeration.
| Protection Type | Allow users to |
| Content | Modify or insert content. |
| DeletingColumns | Delete columns. |
| DeletingRows | Delete rows. |
| Filtering | Set filters. |
| FormattingCells | Format cells. |
| FormattingColumns | Format columns. |
| FormattingRows | Format rows. |
| InsertingColumns | Insert columns. |
| InsertingRows | Insert rows. |
| InsertingHyperlinks | Insert hyperlinks . |
| LockedCells | Select locked cells. |
| UnlockedCells | Select unlocked cells. |
| Objects | Modify drawing objects. |
| Scenarios | Modify saved scenarios. |
| Sorting | Sort data. |
| UsingPivotTables | Use pivot table and pivot chart. |
| All | Do any operations listed above on the protected worksheet. |
| none | Do nothing on the protected worksheet. |
The following steps show you how to protect a worksheet with a specific protection type using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Protect the worksheet with a password and a specific protection type using Worksheet.Protect(password:str, options:SheetProtectionType) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Protect the worksheet with a password and a specific protection type
worksheet.Protect("psd-permission", SheetProtectionType.none)
# Save the workbook to another Excel file
workbook.SaveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Allow Users to Edit Ranges in a Protected Worksheet in Python
In certain cases, you may want to allow users to edit certain ranges of a worksheet while preserving the integrity of other data. The following steps demonstrate how to accomplish this feature using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Specify editable cell ranges using Worksheet.AddAllowEditRange() method.
- Protect the worksheet with a password and a specific protection type using Worksheet.Protect(password:str, options:SheetProtectionType) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Add ranges that allow editing
sheet.AddAllowEditRange("Range One", sheet.Range["A5:A6"])
sheet.AddAllowEditRange("Range Two", sheet.Range["A8:B11"])
# Protect the worksheet with a password and a protection type
sheet.Protect("psd-permission", SheetProtectionType.All)
# Save the workbook to another Excel file
workbook.SaveToFile("AllowEditRange.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Unprotect a Password Protected Worksheet in Python
To remove the protection of a password-protected worksheet, you need to invoke the Worksheet.Unprotect() method and pass the original password to the method as a parameter. The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Remove the password protection using Worksheet.Unprotect(password:str) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook containing protected worksheet
workbook.LoadFromFile("ProtectWorksheet.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Unprotect the worksheet using the specified password
sheet.Unprotect("psd-permission")
# Save the workbook to another Excel file
workbook.SaveToFile("UnprotectWorksheet.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Remove or Reset the Password of an Encrypted Workbook in Python
To remove or reset password of an encrypted workbook, you can use the Workbook.Unprotect() or the Workbook.Protect() method. The following steps show you how to load an encrypted Excel document and delete or change the password of it.
- Create a Workbook object.
- Specify the open password through Workbook.OpenPassword property.
- Load the encrypted Excel file using Workbook.LoadFromFile() method.
- Remove the encryption using Workbook.Unprotect() method. Or change the password using Workbook.Protect() method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Specify the open password
workbook.OpenPassword = "psd-123"
# Load an encrypted Excel workbook
workbook.LoadFromFile("Encrypted.xlsx")
# Unprotect the workbook
workbook.UnProtect()
# Reset password
# workbook.Protect("newpassword")
# Save the workbook to another Excel file
workbook.SaveToFile("UnprotectWorkbook.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
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.

Excel’s AutoFilter feature is a powerful tool that allows you to quickly filter worksheet data based on specific criteria. When applying auto filter to a range of cells, you can display only those rows that meet certain conditions and hide the rest of the data.
However, while filters simplify workflows, knowing how to remove auto filters in Excel is equally critical to maintaining accurate, accessible, and error-free datasets. In this article, you will learn how to add or remove AutoFilters in Excel in Python using Spire.XLS for Python library.
- Installation Guide for Spire.XLS for Python
- How to Use Excel Auto Filters in Python
- How to Remove Auto Filters in Excel
- Conclusion
Installation Guide for Spire.XLS for Python
Spire.XLS for Python is a robust library that enables developers to automate AutoFilter operations in Excel, including adding or removing auto filters.
To install the Python library, open your terminal or command prompt and run the following:
pip install Spire.XLS
The pip tool will search for the latest version of the Spire.XLS library on the Python Package Index (PyPI) and then download and install it along with any necessary dependencies.
How to Use Excel Auto Filters in Python
Add AutoFilter in Excel in Python
Excel AutoFilter can be applied to entire columns or specified cell ranges. The following are the core properties used:
- Worksheet.AutoFilters property: Gets a collection of auto filters in the worksheet, and return an AutoFiltersCollection object.
- AutoFiltersCollection.Range property: Specify the cell range to be filtered.
Code Example:
- Python
from spire.xls import *
from spire.xls.common import *
inputFile = "Data.xlsx"
outputFile = "ExcelAutoFilter.xlsx"
# Create a Workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(inputFile)
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Create an AutoFilter in the sheet and specify the range to be filtered
sheet.AutoFilters.Range = sheet.Range["A1:C1"]
# Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()
Result: Dropdown arrows appear in the header row for filtering.

Different Excel Filter Types in Spire.XLS
The AutoFiltersCollection class of the Spire.XLS for Python library offers various methods for you to filter data in Excel in different ways. Check below for the details:
| Filters | Details |
| Filter text data | Use the AddFilter() to filter cells that contain specified text content. |
| Filter dates | Use the AddDateFilter() method to filter dates associated with the specified year/month/date, etc. |
| Filter blank / non-blank cells |
|
| Filter by color |
|
| Custom filter | Use the CustomFilter() method to filter by the custom criteria. |
Apply Custom Auto Filter in Excel in Python
After adding one of the above filters, you can use the AutoFiltersCollection.Filter() method to apply the filter within the given range. The following is a code example of applying a custom AutoFilter to filter data that is not equal to the specified text string.
- Python
from spire.xls import *
from spire.xls.common import *
inputFile = "Data.xlsx"
outputFile = "CustomFilter.xlsx"
# Create a Workbook instance
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile(inputFile)
#Get the first worksheet
sheet = workbook.Worksheets[0]
# Create an auto filter in the sheet and specify the range to be filtered
sheet.AutoFilters.Range = sheet.Range["B1:B12"]
# Get the column to be filtered
filtercolumn = sheet.AutoFilters[0]
# Add a custom filter to filter data that does not contain the string "Drinks"
strCrt = String("Drinks")
sheet.AutoFilters.CustomFilter(filtercolumn , FilterOperatorType.NotEqual, strCrt)
# Apply the filter
sheet.AutoFilters.Filter()
# Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()
Result: Only cells that are not equal to the string “Drinks” are visible.

How to Remove Auto Filters in Excel in Python
AutoFilters are great for focusing on specific data, but leaving them active can lead to critical issues. Removing auto filters ensures:
- Full data disclosure: All rows/columns are visible.
- Consistent formatting: Eliminates dropdown arrows for a cleaner look.
- Avoid confusion: Prevents recipients from misinterpreting filtered data as the complete dataset.
Spire.XLS for Python provides the AutoFiltersCollection.Clear() method to remove or delete all AutoFilters from an Excel worksheet. Here’s the complete code example:
- Python
from spire.xls import *
from spire.xls.common import *
inputFile = "CustomFilter.xlsx"
outputFile = "RemoveAutoFilter.xlsx"
# Create a Workbook instance
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(inputFile)
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Delete AutoFilter from the sheet
sheet.AutoFilters.Clear()
# Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()
Result: All rows are visible, and AutoFilter dropdowns are removed.

Conclusion
With Spire.XLS for Python, adding or removing auto filters in Excel becomes a seamless, automated process. This guide covered installation, basic and custom filters, and code examples to help you streamline data tasks. For more advanced features, explore the Python Excel library’s full documentation.
Get a Free License
To fully experience the capabilities of Spire.XLS for Python without any evaluation limitations, you can request a free 30-day trial license.
Protecting valuable and sensitive information from unauthorized access is a crucial task for individuals and organizations alike. When it comes to sharing and storing confidential Word documents, such as financial records, legal documents, or personal records, encrypting the documents provides extra protection for their security and confidentiality. Moreover, using Python, users can easily encrypt large numbers of Word documents. This article shows how to use Spire.Doc for Python to encrypt Word documents in Python programs.
- Encrypt a Word Document with a Password
- Change the Password of a Word Document
- Remove the Encryption from a Word Document
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to: How to Install Spire.Doc for Python on Windows
Encrypt a Word Document with a Password
Using the Document.Encrypt(password: str) method provided by Spire.Doc for Python, developers can set an open password for a Word document, ensuring that only authorized people can open and view the document. The detailed steps for encrypting a Word document with a password are as follows:
- Create an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Encrypt the document using Document.Encrypt() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Encrypt the document
doc.Encrypt("password")
# Save the document
doc.SaveToFile("output/EncryptedDocument.docx")
doc.Close()

Change the Encryption from a Word Document
By passing the password as the parameter, developers can load an encrypted document using Document.LoadFromFile(fileName: str, fileFormat: FileFormat, password: str) method. After loading the encrypted document, the Document.Encrypt() method can be used to set a new password. The detailed steps are as follows:
- Create an instance of Document class.
- Load an encrypted Word document using Document.LoadFromFile() method.
- Change the password of the document using Document.Encrypt() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
doc = Document()
# Load an encrypted Word document
doc.LoadFromFile("output/EncryptedDocument.docx", FileFormat.Docx, "password")
# Change the password
doc.Encrypt("password1")
# Save the document
doc.SaveToFile("output/ChangeDocument.docx")
doc.Close()
Remove the Password from a Word Document
After loading an encrypted Word document, developers can also use Document.RemoveEncryption() method to remove the encryption from the document directly, thus making the document available to all users. The detailed steps are as follows:
- Create an instance of Document class.
- Load an encrypted Word document using Document.LoadFromFile() method.
- Remove the password using Document.RemoveEncryption() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
doc = Document()
# Load an encrypted Word document
doc.LoadFromFile("output/EncryptedDocument.docx", FileFormat.Auto, "password")
# Remove the password
doc.RemoveEncryption()
# Save the document
doc.SaveToFile("output/RemovePassword.docx", FileFormat.Docx)
doc.Close()
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.
Flattening forms in PDF means transforming the interactive form fields (such as text boxes, checkboxes, and drop-down menus) into static content. Once a form is flattened, it cannot be edited or filled out anymore. When you need to maintain a permanent and unalterable record of a completed form, flattening is essential. This ensures that the data entered into the form fields cannot be modified or tampered with, providing a reliable reference for future use. In this article, we will demonstrate how to flatten forms in PDF in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Flatten All Forms in a PDF in Python
Spire.PDF for Python provides the PdfDocument.Form.IsFlatten property, which enables you to flatten all forms in a PDF file. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Flatten all forms in the PDF file by setting the PdfDocument.Form.IsFlatten property to True.
- Save the result file using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import * from spire.pdf import * # Specify the input and output PDF file paths input_file = "Form.pdf" output_file = "FlattenAll.pdf" # Create an object of the PdfDocument class doc = PdfDocument() # Load a PDF file doc.LoadFromFile(input_file) # Flatten all forms in the PDF file doc.Form.IsFlatten = True # Save the result file doc.SaveToFile(output_file) doc.Close()

Flatten a Specific Form in a PDF in Python
To flatten a specific form in a PDF file, you can use the PdfField.Flatten property. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF file using the PdfDocument.LoadFromFile() method.
- Get the forms of the PDF file using PdfDocument.Form property.
- Get a specific form by its index or name using PdfFormWidget.FieldsWidget.get_Item() method.
- Flatten the form by setting the PdfField.Flatten property to True.
- Save the result file using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Specify the input and output PDF file paths
input_file = "Form.pdf"
output_file = "FlattenSpecific.pdf"
# Create an object of the PdfDocument class
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile(input_file)
# Get the forms of the PDF file
loadedForm = doc.Form
# Get a specific form by its index or name
formWidget = PdfFormWidget(loadedForm)
form = formWidget.FieldsWidget.get_Item(2)
# form = formWidget.FieldsWidget.get_Item("Address")
# Flatten the specific form
form.Flatten = True
# Save the result file
doc.SaveToFile(output_file)
doc.Close()

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.