[Solved] Create a file inside an open-document

Creating a macro - Writing a Script - Using the API

[Solved] Create a file inside an open-document

Postby vladboscaneanu » Thu Nov 02, 2017 5:15 pm

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
vladboscaneanu
 
Posts: 29
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Postby FJCC » Thu Nov 02, 2017 5:52 pm

Sorry, I am not sure I understand your request. Do you want to export data from the Calc file to a new txt file?
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6155
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create a file inside an open-document

Postby vladboscaneanu » Thu Nov 02, 2017 8:24 pm

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
vladboscaneanu
 
Posts: 29
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Postby Lupp » Sun Nov 05, 2017 1:22 pm

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 5.4.2 and older versions, PortableOpenOffice 4.1.3 and older, StarOffice 5.2
---
Maybe we might! (Create a powerful UFO: United Free Office)
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1520
Joined: Sat May 31, 2014 7:05 pm

Re: Create a file inside an open-document

Postby vladboscaneanu » Sun Nov 05, 2017 2:16 pm

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
vladboscaneanu
 
Posts: 29
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Postby hubert lambert » Sun Nov 05, 2017 4:24 pm

Hello,

Here is a simple example adapted from this script by hanya:
Code: Select all   Expand viewCollapse view
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
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: Create a file inside an open-document

Postby vladboscaneanu » Sun Nov 05, 2017 5:21 pm

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   Expand viewCollapse view
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
vladboscaneanu
 
Posts: 29
Joined: Wed Apr 30, 2014 2:08 am

Re: Create a file inside an open-document

Postby hubert lambert » Sun Nov 05, 2017 5:55 pm

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
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests