Python: Accept or Reject Tracked Changes in Excel

2024-02-01 05:40:02 Written by  support iceblue
Rate this item
(0 votes)

Accepting and rejecting tracked changes in Excel are essential features that empower users to effectively manage and control modifications made by multiple contributors. Accepting changes allows users to include modifications in the spreadsheet, facilitating collaboration and ensuring that the final version reflects collective input. Conversely, rejecting changes enables users to maintain the original content and avoid incorporating incorrect or unnecessary modifications. These functions provide users with the ability to maintain data integrity, ensure document accuracy, and streamline the collaborative process in Excel. In this article, we will demonstrate how to accept and reject tracked changes 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

Accept Tracked Changes in Excel in Python

Spire.XLS for Python provides the Workbook.HasTrackedChanges property to determine whether an Excel workbook has tracked changes or not. If the property returns True, you can use the Workbook.AcceptAllTrackedChanges() method to accept these changes at once.

The following steps explain how to accept all tracked changes in an Excel workbook using Spire.XLS for Python:

  • Create a Workbook object.
  • Load a sample Excel workbook using Workbook.LoadFromFile() method.
  • Check if the workbook has tracked changes using Workbook.HasTrackedChanges property.
  • Accept all tracked changes in the workbook using Workbook.AcceptAllTrackedChanges() method.
  • Save the result workbook using Workbook.SaveToFile() method.
  • Python
from spire.xls.common import *
from spire.xls import *

# Specify the input and output file paths
inputFile = "Sample.xlsx"
outputFile = "AcceptChanges.xlsx"

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(inputFile)

# Check if the file has tracked changes
if workbook.HasTrackedChanges:
    # Accept all tracked changes in the file
    workbook.AcceptAllTrackedChanges()

# Save the result file
workbook.SaveToFile(outputFile, FileFormat.Version2013)
workbook.Dispose()

Python: Accept or Reject Tracked Changes in Excel

Reject Tracked Changes in Excel in Python

If the changes made to a workbook compromise the integrity of the data, such as introducing errors, inconsistencies, or inaccuracies, you can reject these changes by using the Workbook.RejectAllTrackedChanges() method.

The following steps explain how to reject all tracked changes in an Excel workbook using Spire.XLS for Python:

  • Create a Workbook object.
  • Load a sample Excel workbook using Workbook.LoadFromFile() method.
  • Check if the workbook has tracked changes using Workbook.HasTrackedChanges property.
  • Reject all tracked changes in the workbook using Workbook.RejectAllTrackedChanges() method.
  • Save the result workbook using Workbook.SaveToFile() method.
  • Python
from spire.xls.common import *
from spire.xls import *

# Specify the input and output file paths
inputFile = "Sample.xlsx"
outputFile = "RejectChanges.xlsx"

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile(inputFile)

# Check if the file has tracked changes
if workbook.HasTrackedChanges: 
    # Reject all tracked changes in the file
    workbook.RejectAllTrackedChanges()

# Save the result file
workbook.SaveToFile(outputFile, FileFormat.Version2013)
workbook.Dispose()

Python: Accept or Reject Tracked Changes 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