-
-
Save olivx/64d657ff67ecfcc38917f5f34cc1495b to your computer and use it in GitHub Desktop.
Annotations of Pandas DataFrame
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import names\n", | |
"import xlwt\n", | |
"from django.utils.text import slugify" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def gen_persons(max_value):\n", | |
" persons = []\n", | |
" for _ in range(max_value):\n", | |
" full_name = names.get_full_name()\n", | |
" email = '{}@email.com'.format(slugify(full_name))\n", | |
" data = (full_name, email)\n", | |
" persons.append(data)\n", | |
" return persons" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"names_ = gen_persons(100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[('Jeremy Strange', '[email protected]'),\n", | |
" ('Sherry Hampton', '[email protected]'),\n", | |
" ('Daniel Litton', '[email protected]'),\n", | |
" ('Kimberly Horne', '[email protected]'),\n", | |
" ('Erick Cook', '[email protected]')]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"names_[:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def save_xlsx(names_list):\n", | |
" # response = HttpResponse(content_type='application/ms-excel')\n", | |
" date_ = timezone.now().strftime('%y%m%d%H%M%S')\n", | |
" filename = \"/tmp/example.xlsx\"\n", | |
" # response['Content-Disposition'] = 'attachment; filename=%s' % filename\n", | |
" response = filename\n", | |
" workbook = xlwt.Workbook(encoding='utf-8')\n", | |
" sheet = workbook.add_sheet('Persons')\n", | |
" # Sheet header, first row\n", | |
" row_num = 0\n", | |
" font_style = xlwt.XFStyle()\n", | |
" font_style.font.bold = True\n", | |
" columns = ('NAME', 'EMAIL')\n", | |
" for col_num in range(len(columns)):\n", | |
" sheet.write(row_num, col_num, columns[col_num], font_style)\n", | |
"\n", | |
" # Sheet body, remaining rows\n", | |
" font_style = xlwt.XFStyle()\n", | |
" rows = names_list\n", | |
" for row in rows:\n", | |
" row_num += 1\n", | |
" for col_num in range(len(row)):\n", | |
" sheet.write(row_num, col_num, row[col_num], font_style)\n", | |
"\n", | |
" workbook.save(response)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"names_ = gen_persons(100)\n", | |
"save_xlsx(names_)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Django Shell-Plus", | |
"language": "python", | |
"name": "django_extensions" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment