Skip to content

Commit 09c4313

Browse files
authored
Add ability to hide worksheets (#237)
1 parent a18c491 commit 09c4313

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,20 @@ attribute as well. This permits you to modify the style at a later time.
363363
ws[1][1].style.font = font
364364
wb.save("output.xlsx")
365365

366+
Hidden sheet
367+
~~~~~~~~~~~~
368+
369+
PyExcelerate supports adding hidden sheets. Note that the first sheet cannot be hidden.
370+
371+
::
372+
373+
from pyexcelerate import Workbook
374+
375+
wb = Workbook()
376+
ws = wb.new_sheet("visible sheet")
377+
ws = wb.new_sheet("hidden sheet", hidden=True)
378+
wb.save("output.xlsx")
379+
366380
Packaging with PyInstaller
367381
--------------------------
368382

pyexcelerate/Workbook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def add_sheet(self, worksheet):
3333
)
3434
self._worksheets.append(worksheet)
3535

36-
def new_sheet(self, sheet_name, data=None, force_name=False):
37-
worksheet = Worksheet.Worksheet(sheet_name, self, data, force_name)
36+
def new_sheet(self, sheet_name, data=None, force_name=False, hidden=False):
37+
worksheet = Worksheet.Worksheet(sheet_name, self, data, force_name, hidden)
3838
self.add_sheet(worksheet)
3939
return worksheet
4040

pyexcelerate/Worksheet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ class Worksheet(object):
6565
"_attributes",
6666
"_panes",
6767
"_show_grid_lines",
68+
"hidden",
6869
"auto_filter",
6970
)
7071

71-
def __init__(self, name, workbook, data=None, force_name=False):
72+
def __init__(self, name, workbook, data=None, force_name=False, hidden=False):
7273
self._columns = 0 # cache this for speed
7374
if len(name) > 31 and not force_name:
7475
# http://stackoverflow.com/questions/3681868/is-there-a-limit-on-an-excel-worksheets-name-length
@@ -86,6 +87,7 @@ def __init__(self, name, workbook, data=None, force_name=False):
8687
self._attributes = {}
8788
self._panes = Panes.Panes()
8889
self._show_grid_lines = True
90+
self.hidden = hidden
8991
self.auto_filter = False
9092
if data is not None:
9193
# Iterate over the data to ensure we receive a copy of immutables.

pyexcelerate/templates/xl/workbook.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</bookViews>
88
<sheets>
99
{% for index, sheet in workbook.get_xml_data() %}
10-
<sheet name="{{ sheet.name }}" sheetId="{{ index }}" r:id="rId{{ index }}" />
10+
<sheet name="{{ sheet.name }}" sheetId="{{ index }}" r:id="rId{{ index }}" {% if sheet.hidden %}state="hidden" {% endif %}/>
1111
{% endfor %}
1212
</sheets>
1313
<calcPr calcId="145621" />

pyexcelerate/tests/test_Workbook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,11 @@ def test_show_grid_lines():
242242
ws = wb.new_sheet("default_with_grid_lines")
243243
ws[1][1].value = "even more data"
244244
wb.save(get_output_path("grid_lines.xlsx"))
245+
246+
247+
def test_hidden_sheet():
248+
wb = Workbook()
249+
wb.new_sheet("Visible sheet", hidden=False)
250+
wb.new_sheet("Hidden sheet", hidden=True)
251+
wb.new_sheet("Another hidden sheet", hidden=True)
252+
wb.save(get_output_path("hidden_sheet.xlsx"))

0 commit comments

Comments
 (0)