Last active
June 16, 2017 16:53
-
-
Save JoaoFelipe/19419666971bd70c7316aa49e81411d1 to your computer and use it in GitHub Desktop.
Gerador de certificados PySE
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
Nome | Matrícula | |
---|---|---|
Joao | ||
Felipe | 20170616 |
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## O inkscape está travando em algumas conversões para PDF aqui\n", | |
"\n", | |
"Se isso acontecer, apenas reinicie o notebook, e re-execute a última célula" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Instalar inkscapte e colocar no path: https://inkscape.org/pt/\n", | |
"!pip install svgutils" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Sobrescrevendo funções para suportar unicode e fonte colorida\n", | |
"from svgutils.transform import SVGFigure, FigureElement\n", | |
"from lxml import etree\n", | |
"\n", | |
"def fromfile(fname):\n", | |
" fig = SVGFigure()\n", | |
" svg_file = etree.parse(fname)\n", | |
" fig.root = svg_file.getroot()\n", | |
" return fig\n", | |
" \n", | |
" \n", | |
"def save(self, fname):\n", | |
" \"\"\"Save figure to a file\"\"\"\n", | |
" out = etree.tostring(self.root, xml_declaration=True,\n", | |
" standalone=True,\n", | |
" pretty_print=True, encoding=\"UTF-8\")\n", | |
" fid = open(fname, 'wb')\n", | |
" fid.write(out)\n", | |
" fid.close()\n", | |
" \n", | |
"class Text(FigureElement):\n", | |
" \"\"\"Text element.\n", | |
"\n", | |
" Corresponds to SVG ``<text>`` tag.\"\"\"\n", | |
" def __init__(self, x, y, text, size=8, font=\"Verdana\",\n", | |
" weight=\"normal\", letterspacing=0, anchor='start', color=\"black\"):\n", | |
" from svgutils.transform import SVG\n", | |
" txt = etree.Element(SVG+\"text\", {\"x\": str(x), \"y\": str(y),\n", | |
" \"font-size\": str(size),\n", | |
" \"font-family\": font,\n", | |
" \"font-weight\": weight,\n", | |
" \"fill\": color,\n", | |
" \"letter-spacing\": str(letterspacing),\n", | |
" \"text-anchor\": str(anchor)})\n", | |
" txt.text = text\n", | |
" FigureElement.__init__(self, txt)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 Joao\n", | |
"1 Felipe\n" | |
] | |
} | |
], | |
"source": [ | |
"import re\n", | |
"import os\n", | |
"import svgutils.transform\n", | |
"from IPython.display import display, SVG\n", | |
"from unicodedata import normalize\n", | |
"import csv\n", | |
"\n", | |
"TEMPLATE = 'template.svg'\n", | |
"\n", | |
"svgutils.transform.SVG = \"\"\n", | |
"\n", | |
"template = fromfile(TEMPLATE)\n", | |
"width, wunit = re.findall('([\\d\\.]+)(.*)', template.width)[0]\n", | |
"height, hunit = re.findall('([\\d\\.]+)(.*)', template.height)[0]\n", | |
"\n", | |
"# ToDo: definir posição\n", | |
"x = 3550.00 / 2\n", | |
"y = 2550.63 / 2 - 90\n", | |
"\n", | |
"with open('certificados.csv', 'r', encoding='utf-8') as csvfile:\n", | |
" iterador = iter(csv.reader(csvfile, delimiter=','))\n", | |
" next(iterador)\n", | |
" nomes = [(row[0], row[1]) for row in iterador]\n", | |
"if not os.path.exists(\"com_matricula\"):\n", | |
" os.mkdir(\"com_matricula\")\n", | |
"for i, (nome, matricula) in enumerate(nomes):\n", | |
" sem_acento = normalize('NFKD', nome).encode('ASCII','ignore').decode('utf-8')\n", | |
" svgpath = os.path.join(\"com_matricula\", \"{}.svg\".format(sem_acento))\n", | |
" pdfpath = os.path.join(\"com_matricula\", \"{}.pdf\".format(sem_acento))\n", | |
" if os.path.exists(pdfpath):\n", | |
" continue # Pula o que já foi feito\n", | |
" print(i, sem_acento)\n", | |
" template = fromfile(TEMPLATE)\n", | |
" texto = nome\n", | |
" if matricula:\n", | |
" template.append(Text(\n", | |
" x, y - 60, \"{}\".format(nome),\n", | |
" size=130,\n", | |
" anchor=\"middle\",\n", | |
" font=\"Flux\",\n", | |
" weight=\"bold\",\n", | |
" color=\"#306a98\",\n", | |
" letterspacing=0\n", | |
" ))\n", | |
" template.append(Text(\n", | |
" x, y + 50, \"(Matrícula: {})\".format(matricula),\n", | |
" size=100,\n", | |
" anchor=\"middle\",\n", | |
" font=\"Flux\",\n", | |
" weight=\"bold\",\n", | |
" color=\"#306a98\",\n", | |
" letterspacing=0\n", | |
" ))\n", | |
" else:\n", | |
" template.append(Text(\n", | |
" x, y, nome,\n", | |
" size=150.6,\n", | |
" anchor=\"middle\",\n", | |
" font=\"Flux\",\n", | |
" weight=\"bold\",\n", | |
" color=\"#306a98\",\n", | |
" letterspacing=0\n", | |
" ))\n", | |
" \n", | |
" save(template, svgpath)\n", | |
" #template.save(svgpath)\n", | |
" !inkscape --export-pdf=\"{pdfpath}\" \"{svgpath}\"" | |
] | |
}, | |
{ | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## O inkscape está travando em algumas conversões para PDF aqui\n", | |
"\n", | |
"Se isso acontecer, apenas reinicie o notebook, e reexecute a última célula" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Instalar inkscapte e colocar no path: https://inkscape.org/pt/\n", | |
"!pip install svgutils" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import re\n", | |
"import os\n", | |
"import svgutils.transform\n", | |
"from svgutils.transform import fromfile, TextElement\n", | |
"from IPython.display import display, SVG\n", | |
"\n", | |
"# ToDo: alterar para arquivo de template\n", | |
"TEMPLATE = 'template.svg'\n", | |
"\n", | |
"svgutils.transform.SVG = \"\"\n", | |
"\n", | |
"template = fromfile(TEMPLATE)\n", | |
"width, wunit = re.findall('([\\d\\.]+)(.*)', template.width)[0]\n", | |
"height, hunit = re.findall('([\\d\\.]+)(.*)', template.height)[0]\n", | |
"\n", | |
"# ToDo: definir posição\n", | |
"x = float(width) / 2\n", | |
"y = float(height) / 2\n", | |
"\n", | |
"\n", | |
"# ToDO: alterar para leiturar da lista de participantes\n", | |
"nomes = [\n", | |
" \"Anaís\",\n", | |
" \"Bê\",\n", | |
" \"Cássio\",\n", | |
" \"Débora\",\n", | |
" \"Erivàldo\",\n", | |
" \"Feijó\",\n", | |
" \"Grr😬\",\n", | |
" \"Hãõ\",\n", | |
"]\n", | |
"\n", | |
"if not os.path.exists(\"certificados\"):\n", | |
" os.mkdir(\"certificados\")\n", | |
"for i, nome in enumerate(nomes):\n", | |
" svgpath = os.path.join(\"certificados\", \"{}.svg\".format(nome))\n", | |
" pdfpath = os.path.join(\"certificados\", \"{}.pdf\".format(nome))\n", | |
" if os.path.exists(pdfpath):\n", | |
" continue # Pula o que já foi feito\n", | |
" print(i, nome)\n", | |
" template = fromfile(TEMPLATE)\n", | |
" # ToDo: alterar para o formato desejado\n", | |
" template.append(TextElement(\n", | |
" x, y, nome,\n", | |
" size=16,\n", | |
" anchor=\"middle\",\n", | |
" font=\"Verdana\",\n", | |
" weight=\"normal\",\n", | |
" letterspacing=0\n", | |
" ))\n", | |
" \n", | |
" template.save(svgpath)\n", | |
" !inkscape --export-pdf={pdfpath} {svgpath}" | |
] | |
}, | |
{ | |
"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.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment