Skip to content

Commit

Permalink
Update template.py
Browse files Browse the repository at this point in the history
restrict decimal seperator replacement to float fields
  • Loading branch information
gmischler committed Sep 19, 2021
1 parent d70bc37 commit 6ed9686
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ def _parse_colorcode(s):

def parse_csv(self, infile, delimiter=",", decimal_sep=".", encoding=None):
"""Parse template format csv file and create elements dict"""
def varsep_float(s):
"""Convert to float with given decimal seperator"""
# glad to have nonlocal scoping...
return float(s.replace(decimal_sep, '.'))
handlers = (
("name", str.strip),
("type", str.strip),
("x1", float),
("y1", float),
("x2", float),
("y2", float),
("x1", varsep_float),
("y1", varsep_float),
("x2", varsep_float),
("y2", varsep_float),
("font", str.strip),
("size", float),
("size", varsep_float),
("bold", int),
("italic", int),
("underline", int),
Expand All @@ -114,8 +118,6 @@ def parse_csv(self, infile, delimiter=",", decimal_sep=".", encoding=None):
for row in csv.reader(f, delimiter=delimiter):
kargs = {}
for i, v in enumerate(row):
if not v.startswith("'") and decimal_sep != ".":
v = v.replace(decimal_sep, ".")
kargs[handlers[i][0]] = handlers[i][1](v)
self.elements.append(kargs)
self.keys = [v["name"].lower() for v in self.elements]
Expand Down

0 comments on commit 6ed9686

Please sign in to comment.