skip to Main Content

I’m migrating various python scripts from windows to linux (windows 8, debian 10.8, python 3.7); in one of them the results were shown in an excel file:

xlsx_app = r'C:Program FilesLibreOfficeprogramscalc.exe'
Popen ([xlsx_app, fname])

(fname is the path to the excel file)
And now on linux I tried:

p = Popen(['libreoffice', '--calc', fname])

But it just opens calc and then it closes … I’ve already tried using "call", "PIPE" , "shell = False", and it’s always the same – also tried without the ‘–calc’ parameter.

Is there a particular way to open the calc files with python in linux?

Thanks in advance,

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to Rahul Bharadwaj I realized that the problem relies somewhere else... don't know where, but, from this post, someone suggested to use 'xdg-open', which opens any file or url:

    Popen(['xdg-open', fname])
    

    xdg-open uses the application assigned to the file type or url, and I know it would be better to be able to pic the application, but in my case, if the file opens in the expected application (in this case, libreoffice calc) is enough.

    Thanks!


  2. I vaguely remember having an issue with this but no longer remember exactly why I chose this solution.

    >>> from subprocess import Popen
    >>> pid = Popen(['/usr/bin/loffice', 'data.csv'],close_fds=True).pid
    >>> print (pid)
    21948
    

    My gut feeling, is that the close_fds=True parameter was what made the difference.

    If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. Otherwise when close_fds is false, file descriptors obey their inheritable flag as described in Inheritance of File Descriptors.

    Inheritance of File Descriptors

    A file descriptor has an “inheritable” flag which indicates if the file descriptor can be inherited by child processes. Since Python 3.4, file descriptors created by Python are non-inheritable by default.

    On UNIX, non-inheritable file descriptors are closed in child processes at the execution of a new program, other file descriptors are inherited.

    I suppose it doesn’t matter, as you have a solution but I thought I’d throw in what I have used successfully on Linux, to fire up LibreOffice Calc in a separate process.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search