How to create empty LibreOffice file in a current directory on the command line?

I want to ask how it is possible to create any libreoffice file in the current directory of command line. I tried several options from soffice binary help, but all of them just complain about non-existence of file. But, that is the point, I want to create a new file from command line :slight_smile:

The reason why I want to do it, as when using libre office in Windows and saving an unsaved file provides me always last used path, so I have to navigate through complicated file system structure in order to find new location when I want to save a file. It would be much easier when I would just create it by command line file within the current directory.

I am not interested in creating any file (such as by touch), but in creating particular libreoffice file, such as .ods for libre office calc. However, when I create it by touch and open it using libreoffice (thanks to the Windows suffix-based match) it will actually interpret it wrongly and does not preserve any information about the type of the file. After reopening it is just CSV file, and not libreoffice file.

I would expect that soffice binary or scalc binary can do that. But, I did not find out how.

The bugzilla entry already exists since 2011:

This does not look like a LO question. It is rather an OS one and can be considered off-topic. What is your OS?

Under Linux, you just type touch new-file-name

@ajlittoz: I don’t agree that it’s off-topic, because it has to do with how LibreOffice handles new files. However, your solution is correct for Linux. The question mentions Windows, so that’s what I answered.

@jimk: it was not obvious that question was for Windows. OP wrote “as when using W.” which could be interpreted as only an example (remember that both OP and I are non-English native speakers).

@xhomol11: See my edited answer for Calc. Did you look at the link in my original answer?

All that is needed is to create an empty file first. For cmd.exe on Windows:

type nul > new.odt && soffice new.odt

For PowerShell:

$null > new.odt; soffice new.odt

This question was also asked at Open new file from command line?, where a different solution was proposed.


For Calc, the solution in the link will work better. Use LibreOffice to create an empty ODS file somewhere on a short path, such as C:\blank.ods. Then when a new file is needed, enter the following.

copy C:\blank.ods new.ods && soffice new.ods

This was definitely a wrong answer at the time of answering. Creating such an empty file had only resulted in LibreOffice treating the file as plain text file, and when the happy user closed saving formatted document, a warning appeared that saving as Text may result in formatting loss. If user happened to ignore, or even had the warning disabled, they would be surprised when later reopened the file, finding it to loose all formatting. See e.g. tdf#120822.

Only in upcoming version 7.1 this is changed (tdf#123476). Now empty files are opened according to their extensions. So now 0-byte file becomes a valid and easiest option. And it will also work for spreadsheet or presentations, too.

It looks LO is really intended for interactive usage and command line activation is rather painful.

Reading the man page, I tried various strategies. I first moved to a specific working directory so that I could check LO was really pointing to it.

Reminder: I’m under Linux, adapt for Windows.

  • Trying to create a file with LO

     soffice --calc new.ods

    Failure because file does not exist yet

  • Creating file first

     touch empty.ods ; soffice empty.ods

    Failure: Writer opens because file is empty and LO has no clue about future content. Looks like filename is not used for component activation.

  • Creating new file and forcing component

     touch empty.ods ; soffice --calc empty.ods

Calc opens, filename is retrieved from command line but reacts like an import (once again, file being empty, LO has no way to guess its content).

Click OK. An empty sheet opens. Fill in some cells. I check with File>Save as that the current directory is the right one, then Cancel.

Save is requested with File>Save. Calc issues a warning about format Text CSV. This is a consequence of the import operation. Force ODF format with the button to revert to native format. A dialog opens, as if Save as was selected. This is also a consequence of the fake import operation. Confirm replacement.

This needs to be done only at file creation and means user must be present in front of the screen. Maybe some guru knows how to send automatic responses to LO.

  • Opening an existing file

     soffice existing.ods

--calc option is no longer necessary because LO can analyse file content and route to the adequate component.

If this answer helped you, please accept it by clicking the check mark :heavy_check_mark: to the left and, karma permitting, upvote it. If this resolves your problem, close the question, that will help other people with the same question.

Helpful input @ajlittoz – thanks for that. About the italics at end: I wouldn’t recommend “closing” – often someone else will have another approach, or other information to offer. Closing the question inhibits adding quality to a Q&A. (In the absence of active “mods”, though, we make this up as we go along, I suppose!) :confused:

I thought that moderators close a question after accepting an answer to make clear that the answer was not accepted by the user who asked the question. If the OP accepts an answer, there’s no need to close it, is there? Or am I wrong about this?

What you wrote in the answer just confirms that there is no way how to use soffice binary to create a new libre office file. Reinterpretation “casting” is also not correct behavior. Correct behavior is soffice --calc new.ods with potential flag (such as -n) that will result in creating a libre office file. Can we put a request on developing team to take this into consideration?

Feature requests are filed on the dedicated bug site, but don’t expect a quick reaction: there are already so many bugs waiting for fix.

Thanks for an advice, I can file it. Sure, I do not expect it to be fast.

So, I spend all my time at the command line and I kind of gave up on waiting for this. So I wrote this silly little python script to alleviate the pain (at least on Linux): open_new_libreoffice_document/ at master · KennethNielsen/open_new_libreoffice_document · GitHub

It looks for templates on the templates folder (which you can find with the command: xdg-user-dir TEMPLATES) and the templates needs to be named starting with the LO app name such as writer_blanc.odt or writer_company_header.odt thus allowing the user to select from templates while making the new document. You then create the new document with a command such as: calc_blank.ods “My fancy spreadsheet”
to create a new spreadsheet with the name “My fancy spreadsheet” and immediately open it.

To use it, you need to:

  • Know yourself how to make a python script available as a command
  • Have the command xdg-user-dir installed
  • Have the python package argcomplete (which gives command line completion) installed (under python 3 of course)

It isn’t particularly pretty, but if it is of use to someone else, go ahead.

NOTE You should never trust random code on the internet. So you should not execute this or any other script you find on the internet, unless you can read it and ensure yourself that it won’t do any harm.