Class Sfa

class ooodev.io.sfa.Sfa(sfa=None)[source]

Class that can access files using SimpleFileAccess service.

This class can bridge from the document to the file system.

Example

This example copies a file to a new directory in the document. When the document is saved the file will be saved in the document.

from pathlib import Path
from ooodev.io.sfa.sfa import Sfa
# ... other code

sfa = Sfa()
root = f"vnd.sun.star.tdoc:/{doc.runtime_uid}/"
new_dir = root + "new_dir"
sfa.inst.create_folder(new_dir)
myfile = Path("my_file.txt")
s_file = new_dir + "/new_file.txt"
sfa.inst.copy(source_url=myfile.as_uri(), dest_url=s_file)
Parameters:

sfa (XSimpleFileAccess3 | None) –

__init__(sfa=None)[source]

Constructor

Parameters:

sfa (XSimpleFileAccess3 | None, optional) – Simple File Access component. If omitted then new sfa is created. Defaults to None.

delete_file(uri)[source]

Delete file.

Parameters:

uri (str) – The name of the file such as vnd.sun.star.tdoc:/1/Scripts/python/MyFile.py or any other uri format supported by the SimpleFileAccess service.

Return type:

None

exists(file_url)[source]

Checks if a file exists.

Raises:
  • com.sun.star.ucb.CommandAbortedExceptionCommandAbortedException

  • com.sun.star.uno.ExceptionException

Return type:

bool

Parameters:

file_url (str) –

get_folder_contents(folder_url, include_folders=True)[source]

Get the content of a folder (file uri’s).

Parameters:
  • uri (str) – The name of the folder such as vnd.sun.star.tdoc:/1/Scripts/python or any other uri format supported by the SimpleFileAccess service.

  • include_folders (bool, optional) – If True, folders are included in the result. Defaults to True.

  • folder_url (str) –

Returns:

Tuple of file uri’s.

Return type:

Tuple[str, …]

read_text_file(uri)[source]

Read content from file

Parameters:

uri (str) – The name of the file such as vnd.sun.star.tdoc:/1/Scripts/python/MyFile.py or any other uri format supported by the SimpleFileAccess service.

Raises:

FileNotFoundError – If the file is not found.

Returns:

The content.

Return type:

str

write_text_file(uri, content, mode='w')[source]

Write content to file.

Parameters:
  • uri (str) – The name of the file such as vnd.sun.star.tdoc:/1/Scripts/python/MyFile.py or any other uri format supported by the SimpleFileAccess service.

  • content (str) – The content to write.

  • mode (str) – (str, optional): The mode to open the file. Defaults to “w”. mode w will overwrite the file. mode a will append to the file. mode x will create a new file and write to it failing if the file already exists

Return type:

None

DOC_PROTOCOL = 'vnd.sun.star.tdoc'
property inst: SimpleFileAccessComp

Get the SimpleFileAccess component.

Returns:

The SimpleFileAccess component.

Return type:

SimpleFileAccessComp