Skip to content

Instantly share code, notes, and snippets.

@aliyeysides
Last active September 3, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliyeysides/640957976c3fd05e40a3a50b1ddb4113 to your computer and use it in GitHub Desktop.
Save aliyeysides/640957976c3fd05e40a3a50b1ddb4113 to your computer and use it in GitHub Desktop.
create spreadsheet with python
# 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