0

I hope the title makes sense. To give specifics:

I am using csvtotable (https://github.com/vividvilla/csvtotable) to generate HTML tables from CSVs. I have installed via pip and am able to run a command line command:

csvtotable test1743.csv test1743.html

to generate a HTML page. All good so far.

I wanted to do this from within a Python script I had already written so I heard that subprocess was the way to do this. I looked up how to do it and understood that it can be done using the following:

subprocess.run('csvtotable test1743.csv test1743.html',shell=True)

So I tested this via the command line first by doing

python

from the command line and then running

import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)

Success! It worked. Fantastic.

However, when I try to do this from IDLE, it just returns a 1. I have checked the directory thinking that maybe the csv was missing from there, but it still doesn't work.

Am I misunderstanding how subprocess works?

Chris
  • 335
  • 4
  • 14
  • have you tried `subprocess.run(["csvtotable", "test1743.csv", "test1743.html"], shell=True)`? – GuiGWR Jul 05 '18 at 12:36
  • @GuiGWR I just tried it and unfortunately it returns `CompletedProcess(args=['csvtotable', 'test1743.csv', 'test1743.html'], returncode=1)`. I will play around with input and output arguments in subprocess and report back. – Chris Jul 05 '18 at 12:41
  • Maybe you should use [communicate](https://docs.python.org/3/library/subprocess.html?highlight=subprocess#subprocess.Popen.communicate) to look what`s the output of the subprocess – GuiGWR Jul 05 '18 at 12:55
  • Here's a link to an explanation : and a way to manipulate stdout – GuiGWR Jul 05 '18 at 12:55
  • Thanks I will have a look. – Chris Jul 05 '18 at 12:58

1 Answers1

0

Solved by finding a way to call the function without subprocess. I think the issue may have related to default arguments not being set when it is executed through python and hence why below I have had to specify so many arguments.

Code:

from csvtotable import convert

content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)

Note that the argument names had to be changed where they had a - in the name. I just changed any instance e.g. display-length to display_length in convert.py

Chris
  • 335
  • 4
  • 14