How to Hide and Unhide Worksheets in Excel with Python | Easy Guide

Casie LiuCasie Liu
4 min read

Hiding and unhiding worksheets in Excel is a common task when working with spreadsheets. Whether you want to keep certain information private or simply organize your workbook better, Python offers an automatically way to manage worksheet visibility. In this article, I’ll show you how to hide or unhide a worksheet in Excel efficiently, helping you handle large files with ease.

How to Hide a Worksheet in Excel with Python

Hiding worksheets is a great way to protect sensitive information without affecting the overall structure of the Excel workbook. If your file contains data that's not meant for every viewer, hiding specific sheets offers a subtle solution—more discreet than setting up passwords or permissions.

However, if you need a stronger layer of security, you might consider password-protecting your Excel file to fully prevent unauthorized access.

In the next sections, I'll show you how to hide and unhide worksheets using Python. To demonstrate the process, I'll use Spire.XLS, a professional and feature-rich Python library that's available with a free version and free trial.

Steps to hide worksheets in Excel files:

  • Create an object of the Workbook class.

  • Load an Excel file using the Workbook.LoadFromFile() method.

  • Hide a specified worksheet through the Workbook.Worksheets[].Visibility property.

  • Save the modified Excel file with the Workbook.SaveToFile() method.

The code example here shows how to hide the first and the second worksheets in an Excel file:

from spire.xls import *
from spire.xls.common import *

# Create an object of Workbook
workbook = Workbook()

# Load an Excel workbook
workbook.LoadFromFile("/sample files.xlsx")

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

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

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

Python Hide Two Worksheets in Excel

How to Verify If a Worksheet Is Hidden in Excel

Before hiding or unhiding worksheets, it’s often necessary to first check their current visibility status. Especially when working with workbooks you didn’t create yourself, verifying whether a worksheet is hidden can help avoid unnecessary operations. In this part, I’ll show you how to check if a worksheet is hidden or visible using Python.

Steps to check if a worksheet is visible:

  • Instantiate a Workbook instance.

  • Read an Excel workbook from files through the Workbook.LoadFromFile() method.

  • Loop through all worksheets in the Excel file.

  • Check if the current worksheet is hidden.

  • Print out the results.

The code example below shows how to verify if a Worksheet Is hidden:

from spire.xls import *
from spire.xls.common import *


# Create an object of Workbook
workbook = Workbook()


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


# Loop through all worksheets
hidden_sheets = []


for i in range(workbook.Worksheets.Count):
    sheet = workbook.Worksheets[i]
    if sheet.Visibility in (WorksheetVisibility.Hidden, WorksheetVisibility.StrongHidden):
        hidden_sheets.append((i, sheet.Name))


# Print all hidden worksheets
if hidden_sheets:
    print("The following worksheets are hidden:")
    for index, name in hidden_sheets:
        print(f"Index: {index}, Name: {name}")
else:
    print("There are no hidden worksheets.")

Verify If Worksheets are Hidden in Excel with Python

How to Unhide a Worksheet in Excel Using Python

After verifying which worksheets are hidden, the next step is to unhide them if needed. Fortunately, the process is just as simple. You can easily change the worksheet's visibility property from Hidden or StrongHidden back to Visible using the Workbook.Worksheets[].Visibility property.

In the following sections, I’ll walk you through the detailed steps and code examples to unhide worksheets in Excel with Python. Let’s dive in!

Steps to unhide a Worksheet in Excel:

  • Create an instance of the Workbook class.

  • Load a source Excel file through the Workbook.LoadFromFile() method.

  • Unhide a certain worksheet with the Workbook.Worksheets[1].Visibility property.

  • Save the updated Excel spreadsheet using the Workbook.SaveToFile() method.

The Python example below demonstrates how to unhide the second worksheet in an Excel file:

from spire.xls import *
from spire.xls.common import *

# Create an object of Workbook
workbook = Workbook()

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

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

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

The Bottom Line

By now, you’ve learned how to hide, verify, and unhide worksheets in Excel using Python. With these simple techniques, you can better manage your workbook's structure, protect sensitive information, and improve overall readability. Whether you're preparing reports or processing large data files, automating worksheet visibility will make your workflow more efficient and professional.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu