Created
November 2, 2023 03:41
-
-
Save xyb/015ad282967a17d3a5c84f22b7e37644 to your computer and use it in GitHub Desktop.
generate reproducable excel file with openpyxl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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