0

I'm new at using python with the LibreOffice suite. I'm basically trying to programmatically copy a base Impress file and mass copy it but changing the size of certain text boxes.

I checked some documentation online about this but was confused on how to actually achieve it.

Thank you

Edit: I wrote this test code

import os
import zipfile
import glob
import uno

def MassCreatePresentation():

    file = os.path.abspath(glob.glob('INTROTEMPLATE.pptx')[0])
    print('File Found')
    print(file)
    oDoc = XSCRIPTCONTEXT.getDocument()

    return

But it shows this error:

File "MassPresentation.py", line 10, in MassCreatePresentation
    oDoc = XSCRIPTCONTEXT.getDocument()
NameError: name 'XSCRIPTCONTEXT' is not defined

Edit:

Ok I finally figured this out using this logic. The way I did it:

1)get shutil to copy base file 2)use zipfile to unzip the copied pptx file, 3)navigate to the slide xml and use readlines() 4)modify the xml and save it 5)archive as zip and then rename file to .pptx 6)Celebrate

Owlet
  • 47
  • 9
  • https://help.libreoffice.org/6.3/en-US/text/sbasic/python/main0000.html – Joe May 06 '20 at 05:24
  • https://wiki.documentfoundation.org/Macros/Python_Guide/My_first_macro – Joe May 06 '20 at 05:25
  • Does this answer your question? [How to run python macros in LibreOffice?](https://stackoverflow.com/questions/21413664/how-to-run-python-macros-in-libreoffice) – Joe May 06 '20 at 05:25
  • http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html – Joe May 06 '20 at 05:25
  • https://medium.com/analytics-vidhya/starting-libreoffice-with-python-macro-programming-in-openoffice-libreoffice-with-using-10310f9e69f1 – Joe May 06 '20 at 05:58

2 Answers2

0

Since macro recording does not seem possible in Impress, there might be another way that you could try.

LibreOffice files are basically just zip-files with xml or image files in it.

If you unzip the odp-file there is a content.xml inside.

You could process this file using ElementTree or lxml and change the field you need. The font setting are also somewhere in there.

<?xml version="1.0" encoding="UTF-8"?>
  ....
  <draw:text-box><text:list text:style-name="L1">
    <text:list-item>
      <text:p>CHANGE TEXT HERE</text:p>
    </text:list-item></text:list>
  </draw:text-box>
  ....
  <style:text-properties fo:font-family="StarSymbol" fo:color="#666666" fo:font-size="45%"/>

I could not find a good documentation which methods are available using the macro iterface. It might be more elegant, but doing it like this is rather proven to work within one or two hours of work:

  • Unzip
  • Open with ElementTree
  • Change
  • Save the file
  • Zip it back in and update the old one

This page shows some codes to work with zip and LibreOffice files. Also this one.

At best put the content of content.xml through an online formatter like this. So can get a good idea on its structure.

You can assign the font directly or be using one of the defined styles.

This is the definition of L1:

<text:list-style style:name="L1">

within <office:automatic-styles>

This is the usage of that stype:

<text:list text:style-name="L1">

There is only very little information and examples around regarding Impress macros and most of them are in BASIC:

Joe
  • 6,758
  • 2
  • 26
  • 47
  • This is actually a very interesting way to do it. I could basically do it without installing much in my virtual environment. I actually also checked this on .pptx files and the logic is the same just that they are separated by files. I might end up going the .pptx route instead of LibreOffice if it works well enough. I still want to try Jim K answer also. Thank you and I will report back – Owlet May 06 '20 at 20:37
  • I tried this but weirdly when I decompress the pptx file and the modify the xml files and then recompress it it show as a corrupted file and doesn't open anymore – Owlet May 06 '20 at 21:27
  • At best don't extract the whole file, just the one with the content. And then merge it back in. You can add a single file to the zip (pptx) and update it. – Joe May 07 '20 at 04:41
  • Also for pptx there is https://python-pptx.readthedocs.io/en/latest/ – Joe May 07 '20 at 04:42
0

This code changes the font size for a standard ("western") font.

def resize_text_box():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDrawPage = oDoc.getDrawPages().getByIndex(0)
    for oShape in oDrawPage:
        if oShape.supportsService("com.sun.star.drawing.TextShape"):
            oShape.CharHeight = 18

Related: https://stackoverflow.com/a/59934440/5100564

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • I will try this along with the other answer and report back, this seems promising at least if not the answer – Owlet May 06 '20 at 20:38
  • Also what modules do I need to import for this to work? import uno? – Owlet May 06 '20 at 20:45
  • It seems `import uno` is not necessary for this code to work, but it doesn't hurt to import it anyway. Importing successfully shows that python-uno is working, and that helps figure out what is going on if there is a problem. – Jim K May 06 '20 at 21:18
  • I tried the following code oDoc = XSCRIPTCONTEXT.getDocument() But it didn't work it says as error: File "MassPresentation.py", line 11, in MassCreatePresentation oDoc = XSCRIPTCONTEXT.getDocument() NameError: name 'XSCRIPTCONTEXT' is not defined – Owlet May 06 '20 at 21:40
  • Use [APSO](https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-python) to create and execute scripts. Also try [this tutorial](http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html) and [Transfer from Basic to Python](https://wiki.openoffice.org/wiki/Python/Transfer_from_Basic_to_Python). – Jim K May 07 '20 at 17:17