How to use Worksheet.Visible in the xlwings API way

The Visible property of a Worksheet object in Excel, when accessed through the xlwings API, allows you to control the visibility state of a worksheet. This is useful for temporarily hiding sheets from view without deleting them, often to simplify the user interface or to conceal intermediate calculation sheets. A worksheet can be set to be fully visible, completely hidden, or very hidden—the last state means the sheet is hidden and cannot be made visible through the standard Excel user interface (like the “Unhide” dialog), providing an additional layer of concealment.

Syntax:
In xlwings, you interact with the Visible property through a Sheet object (which corresponds to a Worksheet in Excel). The property can be both retrieved and set.

  • Getting the current visibility state:
    sheet.visible
    This returns a string indicating the current state: 'visible', 'hidden', or 'very_hidden'.
  • Setting the visibility state:
    sheet.visible = state
    Here, state is a string that must be one of the following accepted values:
ValueDescription
'visible'Makes the worksheet appear normally in the workbook.
'hidden'Hides the worksheet, but it can be unhidden via Excel’s “Format” > “Hide & Unhide” > “Unhide Sheet” menu.
'very_hidden'Hides the worksheet and removes it from the “Unhide Sheet” dialog. It can only be made visible again programmatically (e.g., via VBA or xlwings).

Code Examples:

  1. Hiding and Unhiding a Sheet:
import xlwings as xw

# Connect to the active workbook
wb = xw.books.active

# Get a reference to a specific sheet
data_sheet = wb.sheets['Sheet1']

# Hide the sheet
data_sheet.visible = 'hidden'
print(f"Sheet1 is now: {data_sheet.visible}") # Output: hidden

# Later, make it visible again
data_sheet.visible = 'visible'
  1. Making a Sheet Very Hidden:
import xlwings as xw

wb = xw.books.active
calc_sheet = wb.sheets['Calculations']

# Hide it so it doesn't appear in the Unhide dialog
calc_sheet.visible = 'very_hidden'

# Check its status
print(calc_sheet.visible) # Output: very_hidden
# To restore it, you must use xlwings again:
# calc_sheet.visible = 'visible'
  1. Iterating Through Sheets and Hiding Based on Name:
import xlwings as xw

wb = xw.books.active

for sheet in wb.sheets:
# Hide all sheets with 'Temp' in their name
if 'Temp' in sheet.name:
    sheet.visible = 'hidden'
    print(f"Hid sheet: {sheet.name}")
  1. Checking if a Sheet is Visible Before Performing an Action:
import xlwings as xw

wb = xw.books.active
report_sheet = wb.sheets['FinalReport']

# Only write data if the sheet is currently visible
if report_sheet.visible == 'visible':
    report_sheet.range('A1').value = 'Report Generated'
else:
    print("Cannot write to a hidden report sheet.")

October 8, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *