losing a digital signature during saving
Created originally on Bitbucket by dmitry kolb (Дмитрий Колб)
Steps to Reproduce:
Microsoft Office 2010, 2013, 2016:
-
Open the file (test.xlsm) that contains the macro project to sign
-
On the Developer tab, in the Code group, click Visual
-
In Visual Basic, on the Tools menu, click Digital Signature
-
The Digital Signature dialog appears, click Choose > select a certificate > click OK
-
Execute the following code:
#!python
from openpyxl import load_workbook
# test.xlsm needs to have the digital signature.
wb = load_workbook('test.xlsm', keep_vba=True)
wb.save('result_test.xlsm')
- need to repeat 1-3 steps for 'result_test.xlsm" to check the signature - the signature will be lost.
I found out that the method _merge_vba (openpyxl/writer/excel.py) controls files which can be added into the excel-archive.
#!python
def _merge_vba(self):
"""
If workbook contains macros then extract associated files from cache
of old file and add to archive
"""
ARC_VBA = re.compile("|".join(
('xl/vba', r'xl/drawings/.*vmlDrawing\d\.vml',
'xl/ctrlProps', 'customUI', 'xl/activeX', r'xl/media/.*\.emf')
)
)
if self.workbook.vba_archive:
for name in set(self.workbook.vba_archive.namelist()) - self.vba_modified:
if ARC_VBA.match(name):
self._archive.writestr(name, self.workbook.vba_archive.read(name))
Excel adds "\xl\_rels\vbaProject.bin.rels" in the archive during adding of the signature. A rule for this file is absent in _merge_vba that's why the digital signature is lost.