Skip to content

Instantly share code, notes, and snippets.

@xyb
Created November 2, 2023 03:41
Show Gist options
  • Save xyb/015ad282967a17d3a5c84f22b7e37644 to your computer and use it in GitHub Desktop.
Save xyb/015ad282967a17d3a5c84f22b7e37644 to your computer and use it in GitHub Desktop.
generate reproducable excel file with openpyxl
# requires:
# pip install openpyxl repro-zipfile
from pathlib import Path
import openpyxl
from repro_zipfile import ReproducibleZipFile
def save_reproducible_excel(path: Path, workbook: openpyxl.Workbook) -> None:
"""save a reproducible/deterministic excel file
>>> excel_path = '/tmp/test.xlsx'
>>> workbook = openpyxl.Workbook()
>>> default_sheet = workbook["Sheet"]
>>> _ = default_sheet.cell(row=1, column=1, value='xyb')
>>> save_reproducible_excel(excel_path, workbook)
>>> from hashlib import sha256
>>> sha256(open(excel_path, 'rb').read()).hexdigest()
'eea770f6547eb611e0e560e6f4876a6e1562754668071c22e4582cfaefa9d29e'
"""
with ReproducibleZipFile(path, "w") as archive:
workbook.properties.modified = datetime.datetime.utcfromtimestamp(0)
workbook.properties.created = datetime.datetime.utcfromtimestamp(0)
writer = ExcelWriter(workbook, archive)
writer.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment