mirror of
https://github.com/sartography/cr-connect-workflow.git
synced 2025-02-24 21:58:32 +00:00
29 lines
836 B
Python
29 lines
836 B
Python
from openpyxl import Workbook
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
from typing import List
|
|
|
|
|
|
class SpreadsheetService(object):
|
|
|
|
@staticmethod
|
|
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
|