Last active
September 3, 2023 16:42
-
-
Save aliyeysides/640957976c3fd05e40a3a50b1ddb4113 to your computer and use it in GitHub Desktop.
create spreadsheet with python
This file contains 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
# python 3.10.3 | |
from xlsxwriter import Workbook | |
def create_xlsx_file(file_path: str, headers: dict, items: list): | |
with Workbook(file_path) as workbook: | |
worksheet = workbook.add_worksheet() | |
worksheet.write_row(row=0, col=0, data=headers.values()) | |
header_keys = list(headers.keys()) | |
for index, item in enumerate(items): | |
row = map(lambda field_id: item.get(field_id, ''), header_keys) | |
worksheet.write_row(row=index + 1, col=0, data=row) | |
xl_headers = { | |
'name': 'Full Name', | |
'email': 'Email', | |
'title': 'Title', | |
} | |
list = [ | |
{'name': 'Ali', 'email': 'ali@email.com', 'title': 'CEO'}, | |
{'name': 'Richard', 'email': 'rich@email.com', 'title': 'CFO'}, | |
{'name': 'Karen', 'email': 'karen@email.com', 'title': 'COO'} | |
] | |
create_xlsx_file("my-xlsx-file.xlsx", xl_headers, list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment