Subprocess.call([soffice]) with macro failing

Sorry if this isn’t really a Libreoffice question. I have a python script that calls soffice to run a macro. It has been working fine before today.

I’m getting this error message, now:

process = subprocess.call([soffice, ‘macro:///DataLog.Module1.Log(’+maindir+’/Template.ods,’+xlsfile+’,’+join(datedir,split(datedir)[1]+".ods")+’)’]) File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 349, in call with Popen(*popenargs, **kwargs) as p: File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 951, in init self._execute_child(args, executable, preexec_fn, close_fds, File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 1420, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

Does anyone have enough experience to help me troubleshoot this? Those line numbers don’t help me any. (_Module1 is only 45 lines long.)

FileNotFoundError seems to suggest that whatever the soffice variable contains cannot be found. It should be a path to an executable. For example, the following code produces the same error:

subprocess.call(["nowhere"])

Please post a reproducible example of your subprocess.call line of code that produces an error. The error message shows several suspicious things:

  • “join” and “split” are not standalone python commands (they should be used like "".join)
  • there are smart quotes ‘ ’
  • we have no idea what the variables contain, especially soffice.

Also your code using + for string concatenation is not good. Build the string properly such as with str.format().

A good modern way to work with paths is pathlib.

I suppose this question would be more appropriate for StackOverflow than this site. However I’m not sure anyone here will be too worried about it. Possibly you would get better answers on other sites, although you will need to improve the question first.

1 Like

You were right; my concatenation was all a mess. I was trying to do way too much on that subprocess.call line. I cleaned it up, and now I know that was not the problem. I have python reporting back what my script determined the subprocess.call should be, and it turns out soffice was pointing to the soffice.exe, and the macro and its parameters are all correct. It’s got to be something else. Could my LO macro’sinteraction with my file system somehow be relying on some python modules (as the error message seems to indicate)? This script/macro worked great up to and including 09/30 of this year. I’ve attached a copy of what’s in the macro.
Untitled 1.odt (17.1 KB)

My error is now:

Traceback (most recent call last): File “C:\Users\Aaron\Documents\Sorting Again.py”, line 94, in process = subprocess.call(call) File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 349, in call with Popen(*popenargs, **kwargs) as p: File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 951, in init self._execute_child(args, executable, preexec_fn, close_fds, File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 1420, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

This time, the call was “([C:\Program Files/LibreOffice/program/soffice.exe, macro:///Datalog.Module1.Log(L:\Data Logger/Template.ods, L\Data Logger ByDate 21-10-04 067317_sample211003 233630 DAD-067330 sa mple211004/123630.DAD.XIS, L Data Logger ByDate 21-10-04L:\Data Logger ByDate 21-10-040ds])”

I think I found the problem, here. Template, ods is quite a suspicious filename.

Actually, it was a miscopy.(That’s what I get for not checking my phone’s transcription of the text in the pic I took.)
Using the print command (instead of pyautogui’s alert command, the call was actually:
([C:\Program Files/LibreOffice/program/soffice.exe, macro:///DataLog.Module1.Log(L:\Data Logger/Template.ods, L:\Data Logger\ByDate\21-10-04\067317_sample211003_233630.DAD-067331_sample211004_133630.DAD.xls, L:\Data Logger\ByDate\21-10-04L:\Data Logger\ByDate\21-10-04ods])

I apologize for how much I’ve been getting in my own way. My python script had some major errors from cleaning up my concatenation. I corrected them, but the problem persists.
This time around, the call is:

([C:\Program Files/LibreOffice/program/soffice.exe, macro:///DataLog.Module1.Log(L:\Data Logger/Template.ods, L:\Data Logger/ByDate/21-10-04\067317_sample211003_233630.DAD-067332_sample211004_143630.DAD.xls, L:\Data Logger/ByDate/21-10-04/21-10-04.ods])

And now, the error is:

File “C:\Users\Aaron\Documents\Sorting Again.py”, line 96, in process = subprocess.call(call) File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 349, in call with Popen(*popenargs, **kwargs) as p: File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 951, in init self._execute_child(args, executable, preexec_fn, close_fds, File “C:\Users\Aaron\AppData\Local\Programs\Python\Python39\lib\subprocess.py”, line 1420, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

I go back to the question, could my LO macro’s interaction with my file system somehow be relying on some python modules (as the error message seems to indicate)?

To get help, you need to provide information in a correctly organized way. Following what you posted, I entered this into a console.

subprocess.call([C:\Program Files/LibreOffice/program/soffice.exe, macro:///DataLog.Module1.Log(L:\Data Logger/Template.ods, L:\Data Logger/ByDate/21-10-04\067317_sample211003_233630.DAD-067332_sample211004_143630.DAD.xls, L:\Data Logger/ByDate/21-10-04/21-10-04.ods])

What a mess! Unsurprisingly it produces an error: SyntaxError: invalid syntax. I’m not even going to attempt to debug it for you, except to suggest that the slashes are inconsistent. Normal slashes / are better because they will not be confused with literal markers, and they work on both Windows and Unix-based systems.

To ask a question correctly, you need to create simplified example code that contains all information necessary to reproduce the problem. It may be too that while creating the simplified example, you will find the answer to your own question.

I don’t see any reason to think that is the problem. The error message gives a trace of the error chain, which is normal.

Yeah, sorry about that. I was for some reason under the impression that when working with Windows, I needed to use a backslash after a drive letter and the colon. I’ll try cleaning that up and see if it helps.

Well, I replaced all my backslashess, and the behavior (and the error messages) are identical. I’m going to have a look at the lines in the .py files mentioned in the error messages. I wish the traceback included more lines, so I could see which part of my LO Basic macro is triggering those python scripts.

I’m kicking myself for not thinking of this before:
Back when it was still workinig, every time the python script tried to use subprocess.call to pull up soffice, it would show the LO splash screen. So,since the errors are all coming up in lines in the subprocess.py file, I don’t think this is even getting through to my LO macro.

That is correct. The error was produced by subprocess.call which is in your python script. Probably FileNotFoundError means that it never finds LO, as I wrote in the first comment under the question.

I would think about replacing this weird ‘subprocess…macro’ stuff with native python code?!

Love to. However, the macro is used to copy a range of cell contents from a .xls file into an .ods template file and save the result to a varying .ods file, and I have no idea how to go about that using native python code.

Solution is given by OP at python - subprocess.call fails with FileNotFoundError at line 1420 - Stack Overflow. The path was changed to “LibreOffice 5” instead of “LibreOffice”.