Skip to content

Commit a77365a

Browse files
committed
Removed the version keyword
1 parent 4445076 commit a77365a

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

xlsxwriter/test/comparison/test_macro05.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ def test_create_file(self):
2727

2828
worksheet = workbook.add_worksheet()
2929

30-
workbook.add_custom_ui(self.vba_dir + "customUI-01.xml", version=2006)
31-
workbook.add_custom_ui(self.vba_dir + "customUI14-01.xml", version=2007)
30+
# Adding a Pre-2010 ribbon
31+
# (XML namespace is http://schemas.microsoft.com/office/2006/01/customui)
32+
workbook.add_custom_ui(self.vba_dir + "customUI-01.xml")
33+
34+
# Adding a Post-2010 ribbon
35+
# (XML namespace is http://schemas.microsoft.com/office/2009/07/customui)
36+
workbook.add_custom_ui(self.vba_dir + "customUI14-01.xml")
3237

3338
workbook.add_signed_vba_project(
3439
self.vba_dir + "vbaProject06.bin",

xlsxwriter/workbook.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,19 +465,36 @@ def use_custom_theme(self, theme: Union[str, os.PathLike, IO[AnyStr]]) -> None:
465465
self.theme_xml = theme_xml
466466
self.default_theme_version = ""
467467

468-
def add_custom_ui(self, custom_ui, version=2007) -> Literal[0, -1]:
468+
def add_custom_ui(
469+
self, custom_ui: Union[str, os.PathLike, IO[AnyStr]]
470+
) -> Literal[0, -1]:
469471
"""
470472
Add a custom UI to the Excel workbook.
471473
472474
Args:
473475
custom_ui: The custom UI xml file name
474-
version: Excel file version for the ribbon (2006 = pre-excel-2014, 2007 = excel 2014)
475-
"""
476476
477+
Returns:
478+
0 on success.
479+
480+
"""
477481
if not os.path.exists(custom_ui):
478482
warn(f"Custom UI xml file '{custom_ui}' not found.")
479483
return -1
480484

485+
# Retrieve the Excel version from the file's namespace:
486+
# (2006 = pre-excel-2014, 2007 = excel 2014)
487+
# namespace declaration is always in the root element
488+
with open(custom_ui, "r", encoding="utf-8") as f:
489+
content = f.read(256)
490+
if "2006/01/customui" in content:
491+
version = 2006
492+
elif "2009/07/customui" in content:
493+
version = 2007
494+
else:
495+
warn(f"Unrecognised or missing xmlns namespace in file '{custom_ui}'.")
496+
return -1
497+
481498
self.custom_uis.append((custom_ui, version))
482499

483500
return 0

0 commit comments

Comments
 (0)