Python: Hide or Unhide Excel Worksheets

2023-09-14 01:03:10 Written by  support iceblue
Rate this item
(0 votes)

The Excel workbook is a powerful spreadsheet that enables the creation, manipulation, and analysis of data in a variety of ways. One of the useful features that workbooks offer is the ability to hide or unhide worksheets in a workbook. Hiding worksheets can help protect sensitive or confidential information, reduce clutter, or organize data more efficiently. And when users need to re-display the hidden worksheets, they can also unhide them with simple operations. This article is going to explain how to hide or unhide worksheets in Excel workbooks through Python programs using Sprie.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

Hide Excel Worksheets in Python

The Worksheet.Visibility property in Spire.XLS for Python can be used to set the visibility of a worksheet. By assigning WorksheetVisibility.Hidden or WorksheetVisibility.StrongHidden to this property, users can change the visibility of a worksheet to hidden or very hidden (completely not shown in Excel and can only be unhidden through code).

The detailed steps for hiding worksheets are as follows:

  • Create an object of Workbook class.
  • Load a workbook using Workbook.LoadFromFile() method.
  • Change the status of the first worksheet to hidden by assigning WorksheetVisibility.Hidden to the Workbook.Worksheets[].Visibility property.
  • Change the status of the second worksheet to very hidden by assigning WorksheetVisibility.StrongHidden to the Workbook.Worksheets[].Visibility property.
  • Save the workbook using Workbook.SaveToFile() method.
  • Python
from spire.common import *
from spire.xls.common import *

# Create an object of Workbook
workbook = Workbook()

# Load an Excel workbook
workbook.LoadFromFile("Sample.xlsx")

# Hide the first worksheet
workbook.Worksheets[0].Visibility = WorksheetVisibility.Hidden

# Change the second worksheet to very hidden
workbook.Worksheets[1].Visibility = WorksheetVisibility.StrongHidden

# Save the workbook
workbook.SaveToFile("output/HideWorksheets.xlsx")

Python: Hide or Unhide Excel Worksheets

Unhide Excel Worksheets in Python

Unhiding a worksheet can be done by assigning WorksheetVisibility.Visible to the Workbook.Worksheets[].Visibility property. The detailed steps are as follows:

  • Create an object of Workbook class.
  • Load a workbook using Workbook.LoadFromFile() method.
  • Unhide the very hidden worksheet by assigning WorksheetVisibility.Visible to the Workbook.Worksheets[].Visibility property.
  • Save the workbook using Workbook.SaveToFile() method.
  • Python
from spire.common import *
from spire.xls.common import *

# Create an object of Workbook
workbook = Workbook()

# Load an Excel workbook
workbook.LoadFromFile("output/HideWorksheets.xlsx")

# Unhide the second worksheet
workbook.Worksheets[1].Visibility = WorksheetVisibility.Visible

# Save the workbook
workbook.SaveToFile("output/UnhideWorksheet.xlsx")

Python: Hide or Unhide Excel Worksheets

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