How to select a textbox in LibreOffice Impress and change it's font size using Python?

893 views Asked by At

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

2

There are 2 answers

4
Joe On BEST ANSWER

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:

5
Jim K On

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