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

Keyboard macros or custom scripts

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

Postby yijinger » Fri Apr 02, 2021 8:51 am

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   Expand viewCollapse view
class WriterTest(object):
    def run(self):
        self.oor = OORunner()
        self.desktop, self.graphicprovider = self.oor.connect()

    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)
        chunk = self.src_doc.getCurrentController().getTransferable()

        result_cursor = self.result_doc.Text.createTextCursor()
        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.)
resulting file (one of)
(18.41 KiB) Downloaded 92 times
Source task file
(35.22 KiB) Downloaded 85 times
Last edited by yijinger on Mon Apr 05, 2021 3:39 pm, edited 1 time in total.
LibreOffice on Linux Mint 20
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

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

Postby yijinger » Mon Apr 05, 2021 3:38 pm

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   Expand viewCollapse view
    def copyframe(self, frame, src_doc, dest_doc):
        cell_enum = frame.Text.createEnumeration()

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

        while cell_enum.hasMoreElements():
            chunk = src_ctrl.getTransferable()
            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 on Linux Mint 20
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests