Skip to content

Commit 6ddcc2e

Browse files
aslehighAdam Lehigh
and
Adam Lehigh
authored
Decode command output to UTF-8 strings. (#40)
Co-authored-by: Adam Lehigh <[email protected]>
1 parent fb66dcf commit 6ddcc2e

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

pypdftk.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def check_output(*popenargs, **kwargs):
4141
def run_command(command, shell=False):
4242
''' run a system command and yield output '''
4343
p = check_output(command, shell=shell)
44-
return p.split(b'\n')
44+
return p.decode("utf-8").splitlines()
4545

4646
try:
4747
run_command([PDFTK_PATH])
@@ -52,8 +52,8 @@ def run_command(command, shell=False):
5252
def get_num_pages(pdf_path):
5353
''' return number of pages in a given PDF file '''
5454
for line in run_command([PDFTK_PATH, pdf_path, 'dump_data']):
55-
if line.lower().startswith(b'numberofpages'):
56-
return int(line.split(b':')[1])
55+
if line.lower().startswith('numberofpages'):
56+
return int(line.split(':')[1])
5757
return 0
5858

5959

@@ -89,11 +89,7 @@ def dump_data_fields(pdf_path):
8989
Return list of dicts of all fields in a PDF.
9090
'''
9191
cmd = "%s %s dump_data_fields" % (PDFTK_PATH, pdf_path)
92-
# Either can return strings with :
93-
# field_data = map(lambda x: x.decode("utf-8").split(': ', 1), run_command(cmd, True))
94-
# Or return bytes with : (will break tests)
95-
# field_data = map(lambda x: x.split(b': ', 1), run_command(cmd, True))
96-
field_data = map(lambda x: x.decode("utf-8").split(': ', 1), run_command(cmd, True))
92+
field_data = map(lambda x: x.split(': ', 1), run_command(cmd, True))
9793
fields = [list(group) for k, group in itertools.groupby(field_data, lambda x: len(x) == 1) if not k]
9894
return [dict(f) for f in fields]
9995

0 commit comments

Comments
 (0)