[Solved] Create a file inside an open-document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

[Solved] Create a file inside an open-document

Post by vladboscaneanu »

Hello.
Can someone ,please, explains how to create and save a txt file in a Calc document,when the last one is running??
I was succesful only when I created a folder,with com.sun.star.ucb.SimpleFileAccess service and
its createFolder function.
Attached Python or Basic code will be welcome.
Thanks in advance.
Last edited by vladboscaneanu on Sun Nov 05, 2017 5:24 pm, edited 1 time in total.
LibreOffice last version on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create a file inside an open-document

Post by FJCC »

Sorry, I am not sure I understand your request. Do you want to export data from the Calc file to a new txt file?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Post by vladboscaneanu »

Thanks for interest.
As we know, each open document is a zip-archive.
I am asking: is there some method to add a simple txt file,
with some text, to the current running document (archive)?
A working snippet would be cool.
LibreOffice last version on Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Create a file inside an open-document

Post by Lupp »

vladboscaneanu wrote:A working snippet would be cool.
Sorry. I don't understand the term 'working snippet'.

Independent of my mentioned problem:

If you manipulate the file AOO will report corruption on the next attempt to open it. (Simply try.)

The open document will accept any insertion by OLE. ('Create from file' e.g.)
You surely find a way to do so by user code. (Why?)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Post by vladboscaneanu »

Hello Lupp.
Thanks for your interesting.
I am looking for a working example,for doing the next task:
-Open a document,Calc for example;
-By macro, create inside this document a textual file.
-Write something to textual file, even "Hello world."
-Save correctly the document.

With python , I can add and write files when the document is CLOSED,using zip library.
I need to add file in a current open document,using the API of LibreOffice.
-------------------------------------------------------------------------------------
I have found this topic that suggest to use
com.sun.star.comp.ucb.TransientDocumentsContentProvider service , but I continuously get "PermessionDenied" error,
when tried to create a file inside of a current document.
That all,thanks in advance for any help.
LibreOffice last version on Windows 10
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Create a file inside an open-document

Post by hubert lambert »

Hello,

Here is a simple example adapted from this script by hanya:

Code: Select all

import os.path
def insert_txt_in_doc(event=None):
    FOLDER = "mytextfolder"
    FILE = "mytextfile.txt"
    TEXT = "Written by\nvladboscaneanu."

    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.ServiceManager
    sfa = smgr.createInstance("com.sun.star.ucb.SimpleFileAccess")

    doc = XSCRIPTCONTEXT.getDocument()
    rep_url = os.path.join(doc.Namespace, FOLDER)
    sfa.createFolder(rep_url)

    file_url = os.path.join(rep_url, FILE)
    io = smgr.createInstance("com.sun.star.io.Pipe")
    text_out = smgr.createInstance("com.sun.star.io.TextOutputStream")
    text_out.setOutputStream(io)
    text_out.setEncoding("UTF-8")
    text_out.writeString(TEXT)
    text_out.closeOutput()
    sfa.writeFile(file_url, io)
This macro add a folder "mytextfolder" in the working document and create a text file with some words.
Error handling is left to you.

Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
vladboscaneanu
Posts: 32
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Post by vladboscaneanu »

Thanks to all for help.
Dear hubert, the code you posted isn't work for me,the error I get is PermissionDenied, on line sfa.CreateFolder.
I suppose this is because of os.path.join and using of doc.Namespace
Changing these two lines ,the code, finally works fine.
The document saves all correctly ,using office API.
Here is your code,with two small changes:

Code: Select all

import os.path
def save_file(*args):
    FOLDER = "mytextfolder"
    FILE = "mytextfile.txt"
    TEXT = "Written by\nvladboscaneanu."

    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.ServiceManager
    sfa = smgr.createInstance("com.sun.star.ucb.SimpleFileAccess")

    doc = XSCRIPTCONTEXT.getDocument()
    rep_url = ('vnd.sun.star.tdoc:/{}/{}'.format(doc.RuntimeUID, FOLDER))
    file_url = '{}/{}'.format(rep_url, FILE)
    #rep_url = os.path.join(doc.Namespace, FOLDER)
    sfa.createFolder(rep_url)

    #file_url = os.path.join(rep_url, FILE)
    io = smgr.createInstance("com.sun.star.io.Pipe")
    text_out = smgr.createInstance("com.sun.star.io.TextOutputStream")
    text_out.setOutputStream(io)
    text_out.setEncoding("UTF-8")
    text_out.writeString(TEXT)
    text_out.closeOutput()
    sfa.writeFile(file_url, io)
Thanks again.
LibreOffice last version on Windows 10
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Create a file inside an open-document

Post by hubert lambert »

vladboscaneanu wrote:I suppose this is because of os.path.join and using of doc.Namespace
Indeed. I wrote this too quickly : the transient uri is available via the Namespace property only for document without location (i.e. newly created).
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply