[Solved][Python][Writer] Copying cell text into another file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
yijinger
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

[Solved][Python][Writer] Copying cell text into another file

Post by yijinger »

Hello, all,

I need to create a Python script which will take tasks from separate files (like "example_src.odt") and make an exercise book file: list tasks firstly (cell "B1"), and then list solutions (cell "B3") with tags (cell "B4"). The problem here is in embedded objects: I could just call "Text.getString()", but it copies plain text, without graphics. I made not a very short way to the following code:

Code: Select all

class WriterTest(object):
    def run(self):
        self.oor = OORunner()
        self.desktop, self.graphicprovider = self.oor.connect()
        self.testCode2()
        self.oor.shutdown()

    def testCode2(self):
        src_url = unohelper.systemPathToFileUrl('/home/yijinger/Projects/OfficeOper/docs/example_src.odt')
        result_url = unohelper.systemPathToFileUrl('/home/yijinger/Projects/OfficeOper/docs/test_result.odt')
        self.src_doc = self.desktop.loadComponentFromURL(src_url, "_default", 0, ())
        self.result_doc = self.desktop.loadComponentFromURL('private:factory/swriter', "_blank", 0, ())

        src_all_tables = self.src_doc.getTextTables()
        src_table = src_all_tables[0]
        task_text = src_table.getCellByName("B3")

        frameProps = (PropertyValue(Name="FrameWidthPercent", Value="100%"),)
        cellContent = task_text.Text.convertToTextFrame(task_text.Text.getStart(), task_text.Text.getEnd(), frameProps)
        self.src_doc.getCurrentController().select(cellContent.getText())
        chunk = self.src_doc.getCurrentController().getTransferable()

        result_cursor = self.result_doc.Text.createTextCursor()
        result_cursor.gotoEnd(False)
        self.result_doc.getCurrentController().insertTransferable(chunk)
        saveArg = PropertyValue(Name="Overwrite", Value=True)
        self.result_doc.storeToURL(result_url, (saveArg,))
OORunner here was taken from here: https://git.libreoffice.org/sdk-example ... UNOPython/ . I finally see something in the result file, but it looks like a very narrow table. I don't know how to extract text content from the cell and paste it without borders. I'm just exhausted and I don't know what to call now. Or maybe I took a totally wrong way. Please help me insert cell content into another file like plain text. (And a picture in "B1" cell into yet another file.)
Attachments
example_result.odt
resulting file (one of)
(18.41 KiB) Downloaded 204 times
example_src.odt
Source task file
(35.22 KiB) Downloaded 196 times
Last edited by yijinger on Mon Apr 05, 2021 3:39 pm, edited 1 time in total.
LibreOffice 6.0.7.3 on Linux Mint 20
yijinger
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

Re: [Python][Writer] Copying cell text into another file

Post by yijinger »

Well, I found a solution yesterday. Maybe it's not ideal, but it works for my task so far. I just got into that TextFrame: it had some TextContent instances (paragraphs really), available through an iterator. So I just copied them one by one with the following method.

Code: Select all

    def copyframe(self, frame, src_doc, dest_doc):
        cell_enum = frame.Text.createEnumeration()

        dest_cursor = dest_doc.Text.createTextCursor()
        dest_cursor.gotoEnd(False)
        dest_ctrl = dest_doc.getCurrentController()
        src_ctrl = src_doc.getCurrentController()

        while cell_enum.hasMoreElements():
            src_ctrl.select(cell_enum.nextElement())
            chunk = src_ctrl.getTransferable()
            dest_ctrl.insertTransferable(chunk)
            dest_cursor.gotoEnd(False)
            dest_doc.Text.insertControlCharacter(dest_cursor, PARAGRAPH_BREAK, 0)
One of side effects of this approach is irreversible conversion from cell content into TextFrame (see string "cellContent = task_text.Text.convertToTextFrame(task_text.Text.getStart(), task_text.Text.getEnd(), frameProps)" in the previous listing), so if I want to use this TextFrame's content in several documents, I need either to hold it in some variable or reopen src_doc and pick necessary cell again.

If someone can improve my solution, it would be great.
LibreOffice 6.0.7.3 on Linux Mint 20
Post Reply