3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

新規開発や新技術の検証、導入にまつわる記事を投稿しよう!

LibreOffice マクロ:Pythonでのプログラム例集

Last updated at Posted at 2023-07-14

情報の少ない Python でのサンプルプログラム。

HelloWorld

最初の Item に "Hello World"

import uno

def HelloWorldPythonItem0():

    oDoc = XSCRIPTCONTEXT.getDocument()
    oSlideList =oDoc.getDrawPages()

    oSlide=oSlideList.getByIndex(0)
    oItem=oSlide.getByIndex(0)
    oItem.String="HelloWorld"

    return None

Writer での挙動
image.png

image.png

表計算 での挙動
image.png

image.png

Impressでの挙動
image.png

Drawでの挙動

image.png

表計算に数値を入力

「LibreOffice マクロを Ubuntu + Python で作る」
https://qiita.com/nanbuwks/items/77d5707b9a2507972676

で書いた、"Hello World via Python" を Calc に表示するプログラムを、別のやりかたで書き換え。
今度はシートやセルの参照方法を変更、文字列の代わりに数値をセルに入力しています。


import uno
def HelloWorldPythonCalc():

    oDoc = XSCRIPTCONTEXT.getDocument()
    
    oSheet = oDoc.CurrentController.getActiveSheet()
    oCell = sheet.getCellRangeByName('A1')
    oCell.Value = '12345'
    
    return None


エクスポート

pdf にエクスポート

import uno
from com.sun.star.beans import PropertyValue

def make_pdf():
    properties=[] 
    p=PropertyValue() 
    p.Name='FilterName' 
    p.Value='calc_pdf_Export' 
    properties.append(p) 
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDoc.storeToURL('file:///tmp/test.pdf',tuple(properties))        
    return True

calc_pdf_Export としていますが、表計算以外でも問題なく pdf になるようです。

選択範囲を png でエクスポート

import uno
from com.sun.star.beans import PropertyValue

def make_png():
    properties=[] 
    p=PropertyValue() 
    p.Name='FilterName' 
    p.Value='calc_png_Export' 
    properties.append(p) 
    p2=PropertyValue() 
    p2.Name='SelectionOnly' 
    p2.Value=True 
    properties.append(p2) 
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDoc.storeToURL('file:///tmp/test.png',tuple(properties))        
    return True

calc_png_Export としていますが、表計算以外でも問題なく png になるようです。

impress

1ページ目を6カラムレイアウトに変更します。

def LayoutChangeCurrentPageTo6ContentImpress():

    oDoc = XSCRIPTCONTEXT.getDocument()
    oSlideList =oDoc.getDrawPages()

    oSlide=oSlideList.getByIndex(0)
    oSlide.Layout=34
    return None

選択したオブジェクトを新しいDrawファイルにコピーして、/tmp/pngsource.odf として保存する

import uno

CTX = uno.getComponentContext()
SM = CTX.getServiceManager()


def call_dispatch(doc, url, args=()):
    frame = doc.getCurrentController().getFrame()
    dispatch = create_instance('com.sun.star.frame.DispatchHelper')
    dispatch.executeDispatch(frame, url, '', 0, args)
    return

def create_instance(name, with_context=False):
    if with_context:
        instance = SM.createInstanceWithContext(name, CTX)
    else:
        instance = SM.createInstance(name)
    return instance

def copySelectedToNewDraw():
    # copy from current draw file
    docsource = XSCRIPTCONTEXT.getDocument()
    call_dispatch(docsource, '.uno:Copy')
    # paste to new draw file
    desktop = create_instance('com.sun.star.frame.Desktop', True)
    path = 'private:factory/sdraw'
    docdist = desktop.loadComponentFromURL(path, '_default', 0, ())
    call_dispatch(docdist, '.uno:Paste')
    # save new draw file to /tmp
    docdist.storeAsURL(convertToURL( "/tmp/pngsource.odg"),())
    docdist.dispose

# https://wiki.openoffice.org/wiki/Danny.OOo.OOoLib.py (LGPL License)
#------------------------------------------------------------
#   General Utility functions
#------------------------------------------------------------

def convertToURL( cPathname ):
    """Convert a Windows or Linux pathname into an OOo URL."""
    if len( cPathname ) > 1:
        if cPathname[1:2] == ":":
            cPathname = "/" + cPathname[0] + "|" + cPathname[2:]
    cPathname = cPathname.replace( "\\", "/" )
    cPathname = "file://" + cPathname
    return cPathname


3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?