Skip to content

Commit

Permalink
python open use default encoding & test for utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Pavlovich committed Aug 22, 2021
1 parent e5d1c69 commit 40345d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions fpdf/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__license__ = "LGPL 3.0"

import csv
import locale
import warnings

from .errors import FPDFException
Expand Down Expand Up @@ -65,7 +66,7 @@ def load_elements(self, elements):
self.elements = elements
self.keys = [v["name"].lower() for v in self.elements]

def parse_csv(self, infile, delimiter=",", decimal_sep="."):
def parse_csv(self, infile, delimiter=",", decimal_sep=".", encoding=None):
"""Parse template format csv file and create elements dict"""
keys = (
"name",
Expand All @@ -88,7 +89,9 @@ def parse_csv(self, infile, delimiter=",", decimal_sep="."):
)
self.elements = []
self.pg_no = 0
with open(infile) as f:
if encoding is None:
encoding = locale.getpreferredencoding()
with open(infile, encoding=encoding) as f:
for row in csv.reader(f, delimiter=delimiter):
kargs = {}
for i, v in enumerate(row):
Expand Down
1 change: 1 addition & 0 deletions fpdf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def try_to_type(value):
pass
return value


def substr(s, start, length=-1):
if length < 0:
length = len(s) - start
Expand Down
5 changes: 5 additions & 0 deletions test/template/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ def test_template_nominal_csv(tmp_path):
tmpl.add_page()
tmpl.render()
assert_pdf_equal(tmpl.pdf, HERE / "template_nominal_csv.pdf", tmp_path)
tmpl = Template(format="A4", title="Sample Invoice")
tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";", encoding="utf-8")
tmpl.add_page()
tmpl.render()
assert_pdf_equal(tmpl.pdf, HERE / "template_nominal_csv.pdf", tmp_path)


def test_template_code39(tmp_path): # issue-161
Expand Down

0 comments on commit 40345d4

Please sign in to comment.