2017-2018/Notes/DB vers xlsx.ipynb

759 lines
18 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import xlsxwriter"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from repytex.tools.evaluation import Evaluation\n",
"from repytex.tools.sqlite_getters import all_ev_description\n",
"import sqlite3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"conn = sqlite3.connect('./recopytex.db')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>name</th>\n",
" <th>term</th>\n",
" <th>t_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>DS1 mise en jambe</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>DS1</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>DS1</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>DS2</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>DS2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>6</td>\n",
" <td>DS2</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>7</td>\n",
" <td>DS3</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>8</td>\n",
" <td>Pyramide de pièces</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>9</td>\n",
" <td>DS3</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>10</td>\n",
" <td>DS3</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>11</td>\n",
" <td>DS4</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>13</td>\n",
" <td>DS5</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>14</td>\n",
" <td>DM noel</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>15</td>\n",
" <td>DM noel</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>16</td>\n",
" <td>DS4</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>17</td>\n",
" <td>DS4</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>18</td>\n",
" <td>DS6</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>19</td>\n",
" <td>CM1</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>20</td>\n",
" <td>DNB blanc1</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>21</td>\n",
" <td>DNB blanc1</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id name term t_id\n",
"0 1 DS1 mise en jambe 1 1\n",
"1 2 DS1 1 2\n",
"2 3 DS1 1 3\n",
"3 4 DS2 1 2\n",
"4 5 DS2 1 1\n",
"5 6 DS2 1 3\n",
"6 7 DS3 1 3\n",
"7 8 Pyramide de pièces 1 1\n",
"8 9 DS3 1 2\n",
"9 10 DS3 1 1\n",
"10 11 DS4 2 3\n",
"11 13 DS5 2 3\n",
"12 14 DM noel 2 1\n",
"13 15 DM noel 2 2\n",
"14 16 DS4 2 1\n",
"15 17 DS4 2 2\n",
"16 18 DS6 2 3\n",
"17 19 CM1 2 2\n",
"18 20 DNB blanc1 2 2\n",
"19 21 DNB blanc1 2 1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_ev_description(conn)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"ev = Evaluation.from_sqlite(21, conn)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'DNB blanc1'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ev.name"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" <th>name</th>\n",
" <th>eval_id</th>\n",
" <th>date</th>\n",
" <th>comment</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>64</td>\n",
" <td>1 - QCM</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>65</td>\n",
" <td>2 - Boite de chocolat</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>66</td>\n",
" <td>3 - Scratch</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>67</td>\n",
" <td>4 - Programme de calcul</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>68</td>\n",
" <td>5 - Macaron</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>69</td>\n",
" <td>6 - Inspecteur</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>70</td>\n",
" <td>7 - La tache</td>\n",
" <td>21</td>\n",
" <td>2018-02-07 00:00:00.000000</td>\n",
" <td></td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id name eval_id date comment\n",
"0 64 1 - QCM 21 2018-02-07 00:00:00.000000 \n",
"1 65 2 - Boite de chocolat 21 2018-02-07 00:00:00.000000 \n",
"2 66 3 - Scratch 21 2018-02-07 00:00:00.000000 \n",
"3 67 4 - Programme de calcul 21 2018-02-07 00:00:00.000000 \n",
"4 68 5 - Macaron 21 2018-02-07 00:00:00.000000 \n",
"5 69 6 - Inspecteur 21 2018-02-07 00:00:00.000000 \n",
"6 70 7 - La tache 21 2018-02-07 00:00:00.000000 "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ev.ex_df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"wb = xlsxwriter.Workbook(\"DNB_BLANC_pro1.xlsx\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formatages"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eval_format = wb.add_format({'bold': True, 'font_color': 'red'})\n",
"eval_cell = wb.add_format()\n",
"eval_cell.set_bg_color(\"red\")\n",
"# exo_cell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"La feuille de calcul"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"wb = xlsxwriter.Workbook(\"DS4_302.xlsx\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ws = wb.add_worksheet()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"En tête du tableau"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ws.write(0,1,\"Competence\")\n",
"ws.write(0,2,\"Barème\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Toutes les questions et exercices"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def q_line(q, row, ws):\n",
" ws.write(row, 0, f\"{q.name} - {q.comment}\")\n",
" ws.write(row, 1, f\"{q.competence}\")\n",
" ws.write(row, 2, q.score_rate)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def exo_block(exo, row, ws):\n",
" qrow = row\n",
" for q in exo[\"questions\"]:\n",
" qrow += 1\n",
" q_line(q, qrow, ws)\n",
" ws.write(row, 0, f\"Ex {exo['name']}\")\n",
" row_range = \", \".join([f\"C{i}\" for i in range(row+2, qrow+2)])\n",
" for i in range(30):\n",
" ws.write(row, i+2, f\"=SUM({row_range})\")\n",
" return qrow"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
"source": [
"row = 2\n",
"exo_rows = []\n",
"for exo in ev.exercises:\n",
" exo_rows.append(row)\n",
" row = exo_block(exo, row, ws) + 1\n",
"ws.write(1,0, ev.name, eval_format)\n",
"exo_xls_range = \", \".join([f\"C{i+1}\" for i in exo_rows])\n",
"for i in range(30):\n",
" ws.write(1,i+2, f\"=SUM({exo_xls_range})\", eval_format)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"wb.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Avec openpyxl"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from openpyxl import Workbook\n",
"from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, colors"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'00FF0000'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"colors.RED"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"wb = Workbook()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ws = wb.create_sheet(ev.name)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Cell 'DNB blanc1'.C1>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ws.cell(1,2,\"Competence\")\n",
"ws.cell(1,3,\"Barème\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def q_line(q, row, ws):\n",
" ws.cell(row, 1, f\"{q.name} - {q.comment}\")\n",
" ws.cell(row, 2, f\"{q.competence}\")\n",
" ws.cell(row, 3, q.score_rate)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def exo_block(exo, row, ws):\n",
" qrow = row\n",
" for q in exo[\"questions\"]:\n",
" qrow += 1\n",
" q_line(q, qrow, ws)\n",
" ws.cell(row, 1, f\"Ex {exo['name']}\")\n",
" row_range = \", \".join([f\"C{i}\" for i in range(row+1, qrow+1)])\n",
" ws.cell(row, 3, f\"=SUM({row_range})\")\n",
" return qrow"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Cell 'DNB blanc1'.C2>"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"row = 3\n",
"exo_rows = []\n",
"for exo in ev.exercises:\n",
" exo_rows.append(row)\n",
" row = exo_block(exo, row, ws) + 1\n",
"eval_row = ws.row_dimensions[2]\n",
"eval_row.fill = PatternFill(bgColor=colors.RED)\n",
"ws.cell(2,1, ev.name)\n",
"exo_xls_range = \", \".join([f\"C{i}\" for i in exo_rows])\n",
"ws.cell(2,3, f\"=SUM({exo_xls_range})\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'302'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ev.tribe"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"wb.save(f\"./{ev.name}-{ev.tribe}.xlsx\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}