cr-connect-workflow/crc/services/spreadsheet_service.py

29 lines
836 B
Python
Raw Normal View History

from openpyxl import Workbook
from tempfile import NamedTemporaryFile
2022-03-11 18:02:37 -05:00
from typing import List
class SpreadsheetService(object):
@staticmethod
2022-03-11 18:02:37 -05:00
def create_spreadsheet(data: List[dict], headers: List[str] = None, title: str = None):
"""The length of headers must be the same as the number of items in the dictionaries,
and the order must match up.
The title is used for the worksheet, not the filename."""
wb = Workbook(write_only=True)
ws = wb.create_sheet()
if title:
ws.title = title
if headers:
ws.append(headers)
for row in data:
ws.append(list(row.values()))
with NamedTemporaryFile() as tmp:
wb.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
return stream