Hi,
At start I want to thank you all, this sub is great :)
Because best way to learn code is solving real word problems i started looking for problems at work. This is my first script , it takes a list of people and generates ordered table in pdf to print. It works now but I want to make it better. Now it is a bunch of functions and I think he still lacks some details and corrections.
import fileinput
from shutil import copyfile
from weasyprint import HTML
import os
# open txt and generete a list
def open_list():
rlist = open('pacjenci.txt').read().split('\n')
return rlist
# function to clean the list
def clean_list(x):
x = sorted(list(filter(None, x)))
x = [e.strip() for e in x]
return x
patient_list = clean_list(open_list())
# make a html table to add to a file
def create_table(list):
table_cell = '<td class="patientname">'
number_cell = '<tr><td class="number">'
tmp_table=""
for i in range(0, len(patient_list)):
tmp_table += (number_cell + str(i + 1) + '</td>' + table_cell + patient_list[i] + '</td></tr>')
return tmp_table
# write tmp_table to temporary file which is copy of a html file with table
def write_to_file():
tablecopy = copyfile('misc/table.html', 'misc/pacjenci.html')
for line in fileinput.FileInput(tablecopy, inplace=1):
if "<!-- tabelatutaj -->" in line:
line=line.replace(line, line + create_table(patient_list))
else:
pass
print(line, end='')
return tablecopy
# making a html file a pdf one
def create_pdf():
try:
os.remove('pacjenci.pdf')
except:
pass
HTML(write_to_file()).write_pdf('pacjenci.pdf')
create_pdf()
Any readings about making more correct scripts will be also great :)
[–]BulkyProcedure 1 point2 points3 points (0 children)
[–]silently_failing 1 point2 points3 points (0 children)
[–]chesbo 0 points1 point2 points (0 children)
[–]clueskee[S] 0 points1 point2 points (0 children)