Skip to content

Commit

Permalink
Add ability to hide worksheets (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
9thbit authored Jun 10, 2024
1 parent a18c491 commit 09c4313
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ attribute as well. This permits you to modify the style at a later time.
ws[1][1].style.font = font
wb.save("output.xlsx")

Hidden sheet
~~~~~~~~~~~~

PyExcelerate supports adding hidden sheets. Note that the first sheet cannot be hidden.

::

from pyexcelerate import Workbook

wb = Workbook()
ws = wb.new_sheet("visible sheet")
ws = wb.new_sheet("hidden sheet", hidden=True)
wb.save("output.xlsx")

Packaging with PyInstaller
--------------------------

Expand Down
4 changes: 2 additions & 2 deletions pyexcelerate/Workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def add_sheet(self, worksheet):
)
self._worksheets.append(worksheet)

def new_sheet(self, sheet_name, data=None, force_name=False):
worksheet = Worksheet.Worksheet(sheet_name, self, data, force_name)
def new_sheet(self, sheet_name, data=None, force_name=False, hidden=False):
worksheet = Worksheet.Worksheet(sheet_name, self, data, force_name, hidden)
self.add_sheet(worksheet)
return worksheet

Expand Down
4 changes: 3 additions & 1 deletion pyexcelerate/Worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ class Worksheet(object):
"_attributes",
"_panes",
"_show_grid_lines",
"hidden",
"auto_filter",
)

def __init__(self, name, workbook, data=None, force_name=False):
def __init__(self, name, workbook, data=None, force_name=False, hidden=False):
self._columns = 0 # cache this for speed
if len(name) > 31 and not force_name:
# http://stackoverflow.com/questions/3681868/is-there-a-limit-on-an-excel-worksheets-name-length
Expand All @@ -86,6 +87,7 @@ def __init__(self, name, workbook, data=None, force_name=False):
self._attributes = {}
self._panes = Panes.Panes()
self._show_grid_lines = True
self.hidden = hidden
self.auto_filter = False
if data is not None:
# Iterate over the data to ensure we receive a copy of immutables.
Expand Down
2 changes: 1 addition & 1 deletion pyexcelerate/templates/xl/workbook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</bookViews>
<sheets>
{% for index, sheet in workbook.get_xml_data() %}
<sheet name="{{ sheet.name }}" sheetId="{{ index }}" r:id="rId{{ index }}" />
<sheet name="{{ sheet.name }}" sheetId="{{ index }}" r:id="rId{{ index }}" {% if sheet.hidden %}state="hidden" {% endif %}/>
{% endfor %}
</sheets>
<calcPr calcId="145621" />
Expand Down
8 changes: 8 additions & 0 deletions pyexcelerate/tests/test_Workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,11 @@ def test_show_grid_lines():
ws = wb.new_sheet("default_with_grid_lines")
ws[1][1].value = "even more data"
wb.save(get_output_path("grid_lines.xlsx"))


def test_hidden_sheet():
wb = Workbook()
wb.new_sheet("Visible sheet", hidden=False)
wb.new_sheet("Hidden sheet", hidden=True)
wb.new_sheet("Another hidden sheet", hidden=True)
wb.save(get_output_path("hidden_sheet.xlsx"))

0 comments on commit 09c4313

Please sign in to comment.