LoginSignup
4
1

More than 5 years have passed since last update.

LibreOffice Pythonマクロ サンプルプログラム(初級者向け)Writer編 No.1

Last updated at Posted at 2018-02-28

注意)
このページのマクロは、Libreofficeの「マクロの実行」で動作させます。

LibreOfficeでPythonマクロを実行するには、
UbuntuのLibreOffice(MS Office互換性あり)でPythonマクロを使用する方法(超初級者向け)
https://qiita.com/ty21ky/items/2c8b001363c8293198e2
を参照ください。

環境

  Ubuntu Studio 17.10
  LibreOffice6.0

4.Macroの杜(Sample Macro)

http://openoffice3.web.fc2.com/index.html
よく見ると、Macroの杜にはPython編 Writerのサンプルマクロがありません。

OpenOffice.org / LibreOffice Basic編 Writer

No.1
http://openoffice3.web.fc2.com/OOoBasic_Writer.html
No.2
http://openoffice3.web.fc2.com/OOoBasic_Writer_No2.html
を参考に、勉強がてらサンプルマクロを作成していきます。
ここには、コードだけを書きますので上記のBasicコードと一緒に見ながらやってください。
簡単なものから、出来る範囲内で行います。

実行して確認したものだけを記載していきますが、もしおかしな所があれば指摘していただけると嬉しいです。

*******************【 Macro Code 】*******************

File

[ Open / Close ]

WF-1)[Writer]新規にWriter fileを開く(2018/3/11)

oWriterOpen.py
import uno
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX

def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
    """shows message."""
    desktop = XSCRIPTCONTEXT.getDesktop()
    frame = desktop.getCurrentFrame()
    window = frame.getContainerWindow()
    toolkit = window.getToolkit()
    msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
    return msgbox.execute()

#File
#[ Open / Close ]
#WF-1)[Writer]新規にWriter fileを開く
def oWriterOpen():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # open a writer document
    oDoc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())

    oAns = omsgbox(oMessage="ファイルを閉じますか?", oBtnType = 3, oTitle = "File Close確認")
    if oAns == 2:
        oDoc.dispose()

WF-2)[Writer]新規Writer fileの開閉(保存確認有り)(2018/3/11)
注意)
from InputMessageBox import inputbox
がエラーになる場合があります。UbuntuStdioではエラーにならないのですが、LinuxMintではエラーになります???
この場合、from InputMessageBox import inputboxの前に「#」を入れてコメントにして
https://qiita.com/ty21ky/items/6070987d962cb0cdaea8
の一番下に、InputBoxメソッドのコードがあるので、それを挿入すると動きました。

oWriterOpen_Save.py
import uno
import unohelper
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from InputMessageBox import inputbox
from com.sun.star.beans import PropertyValue
import os.path

def oWriterOpen_Save():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # open a writer document
    oDoc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())

    oAns = omsgbox(oMessage="fileを保存しますか?", oBtnType = 3, oTitle = "File Save確認")
    if oAns == 2:
        oInp = inputbox("Full pathでFile nameを入力して下さい(例 : /home/ユーザー名/btest.odt)", "保存File nameの入力", "", 200, 200)
        if oInp != None:
            ofile= os.path.abspath(oInp)
            oURL = unohelper.systemPathToFileUrl(ofile)
            p = PropertyValue()
            p.Name = 'write'
            p.Value = True
            properties = (p,)
            oDoc.storeAsURL(oURL, properties)

        oAns = omsgbox(oMessage="ファイルを閉じますか?", oBtnType = 3, oTitle = "File Close確認")
        if oAns == 2:
            oDoc.dispose()

WF-3)[Writer]新規にHTML形式 fileを開く

oWriter_HTML_Web_Doc.py
import uno

def openWriterHtml():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # open a writer document
    doc = desktop.loadComponentFromURL("private:factory/swriter/web", "_blank", 0, ())

WF-4)[Writer]新規にMaster Document(odmL形式) fileを開く

oGlobalDoc.py
import uno

def openWriterHtml():
    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # open a writer document
    doc = desktop.loadComponentFromURL("private:factory/swriter/GlobalDocument", "_blank", 0, ())

[ File Property ]

WDPp-)

WDPp-)

Document

[ Font ]

WDF-)[Writer]文字列の右1文字を上付文字にする

DocFont.py
def DocFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oText.String = "水素はH2"
    oTextCursor = oText.createTextCursor()
    oTextCursor.gotoEnd( False )
    oTextCursor.goLeft(1,True)                                  #←1文字
    oTextCursor.setPropertyValue( "CharEscapement",101 )        #←上付きは101
    oTextCursor.setPropertyValue( "CharEscapementHeight", 60 )  #←60%
    oTextCursor.gotoEnd( False )
    #メッセージボックスは他にたくさんあるので省略

WDF-)[Writer]文字列の左1文字を下付文字にする

DocFont1.py
def DocFont1( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oText.String = "水素はH2"
    oTextCursor = oText.createTextCursor()
    oTextCursor.gotoEnd( False )
    oTextCursor.goLeft(1,True)                                  #←1文字
    oTextCursor.setPropertyValue( "CharEscapement",-101 )        #←上付きは101
    oTextCursor.setPropertyValue( "CharEscapementHeight", 60 )  #←60%
    oTextCursor.gotoEnd( False )
    #メッセージボックスは他にたくさんあるので省略

WDF-)[Writer]英文字を80、日本語を40サイズにする

WriterFont.py
import uno
#import unohelper  #不要(2018/3/11)
#import sys        #不要(2018/3/11)
import traceback
from com.sun.star.awt.MessageBoxType import MESSAGEBOX

def omsgbox(oMessage='',oBtnType=1,oTitle='Title',oMsgType='messbox'):
    """shows message."""
    desktop = XSCRIPTCONTEXT.getDesktop()
    frame = desktop.getCurrentFrame()
    window = frame.getContainerWindow()
    toolkit = window.getToolkit()
    msgbox = toolkit.createMessageBox(window, MESSAGEBOX, oBtnType, oTitle, oMessage)
    return msgbox.execute()

def WriterFont( ):
  try:
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=80
    oTextCursor.CharHeightAsian=40
    oText.String = "ABCDEFGこれはテストです"    #Writerは先に書式設定する必要有
  except Exception as er:
    oDisp = ''
    oDisp = str(traceback.format_exc()) + '\n' + str(er)
    omsgbox(oDisp)

On Error Goto関係(例外)は「Transfer from Basic to Python」に記載されているコードを貼り付けるとエラーになります。
以後、例外の処理とメッセージボックスは省略します。

WDF-)[Writer]文字Font

WriterFont.py
def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharFontName = "Arial"
    oTextCursor.CharFontNameAsian = "Arial"
    oTextCursor.CharHeight=20
    oTextCursor.CharHeightAsian=20
    oText.String="AbcDe12345これはテストです"

WDF-)[Writer]Itaric

WriterFont.py
import uno

from com.sun.star.awt.FontPitch import FIXED, VARIABLE
from com.sun.star.awt.FontSlant import ITALIC

def WriterFont1( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharFontPitch = uno.getConstantByName("com.sun.star.awt.FontPitch.FIXED")   # FIXED と VARIABLEとも結果は同じ?
    oTextCursor.CharFontPitch = uno.getConstantByName("com.sun.star.awt.FontPitch.VARIABLE")
    #oTextCursor.CharPosture = uno.getConstantByName("com.sun.star.awt.FontSlant.ITALIC")
    oTextCursor.CharPosture = "Italic"
    #oTextCursor.CharPostureAsian = uno.getConstantByName("com.sun.star.awt.FontSlant.ITALIC")
    oTextCursor.CharPostureAsian = "Italic"
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

uno.getConstantByName("com.sun.star.awt.FontSlant.ITALIC")
だと、いくらやってもエラーになるので、
"Italic"
だと駄目なのかな?一応Itaricになっているけど。

WDF-)[Writer]OBLIQUE

WriterFont.py
import uno

from com.sun.star.awt.FontPitch import FIXED, VARIABLE
from com.sun.star.awt.FontSlant import OBLIQUE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharFontPitch = uno.getConstantByName("com.sun.star.awt.FontPitch.FIXED")   # FIXED と VARIABLEとも結果は同じ?
    oTextCursor.CharFontPitch = uno.getConstantByName("com.sun.star.awt.FontPitch.VARIABLE")
    #oTextCursor.CharPosture = uno.getConstantByName("com.sun.star.awt.FontSlant.OBLIQUE")
    oTextCursor.CharPosture = "Oblique"
    #oTextCursor.CharPostureAsian = uno.getConstantByName("com.sun.star.awt.FontSlant.OBLIQUE")
    oTextCursor.CharPostureAsian = "Oblique"
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

これも
uno.getConstantByName("com.sun.star.awt.FontSlant.OBLIQUE")
がエラーになるので、"Oblique"にしています。

上のItaricとの違いがわからない?

WDF-)[Writer]REVERSE_ITALIC

WriterFont.py
import uno

from com.sun.star.awt.FontSlant import REVERSE_ITALIC

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    #oTextCursor.CharPosture = uno.getConstantByName("com.sun.star.awt.FontSlant.REVERSE_ITALIC")
    oTextCursor.CharPosture = "Reverse_Italic"
    #oTextCursor.CharPostureAsian = uno.getConstantByName("com.sun.star.awt.FontSlant.REVERSE_ITALIC")
    oTextCursor.CharPostureAsian = "Reverse_Italic"
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]REVERSE_OBLIQUE

WriterFont.py
import uno

from com.sun.star.awt.FontSlant import REVERSE_OBLIQUE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    #oTextCursor.CharPosture = uno.getConstantByName("com.sun.star.awt.FontSlant.REVERSE_OBLIQUE")
    oTextCursor.CharPosture = "Reverse_Oblique"
    #oTextCursor.CharPostureAsian = uno.getConstantByName("com.sun.star.awt.FontSlant.REVERSE_OBLIQUE")
    oTextCursor.CharPostureAsian = "Reverse_Oblique"
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]BOLD

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import BOLD

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD")
    #oTextCursor.CharWeight = "Bold" #これはエラーで使用できない
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD")
    #oTextCursor.CharWeightAsian = "Bold" #これはエラーで使用できない
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]SEMIBOLD

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import SEMIBOLD

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.SEMIBOLD")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.SEMIBOLD")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]ULTRABOLD

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import ULTRABOLD

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]BLACK

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import BLACK

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.BLACK")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.BLACK")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]THIN

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import THIN

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.THIN")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.THIN")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]ULTRALIGHT

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import ULTRALIGHT

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRALIGHT")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRALIGHT")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]LIGHT

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import LIGHT

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.LIGHT")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.LIGHT")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]SEMILIGHT

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import SEMILIGHT

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.SEMILIGHT")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.SEMILIGHT")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]SINGLE(下線)

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import SINGLE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.SINGLE")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DOUBLE(下線)

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DOUBLE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DOUBLE")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DOTTED

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DOTTED

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DOTTED")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DASH

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DASH

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DASH")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]LONGDASH

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import LONGDASH

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.LONGDASH")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DASHDOT

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DASHDOT

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DASHDOT")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DASHDOTDOT

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DASHDOTDOT

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DASHDOTDOT")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]SMALLWAVE

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import SMALLWAVE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.SMALLWAVE")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]WAVE

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import WAVE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.WAVE")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]DOUBLEWAVE

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DOUBLEWAVE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DOUBLEWAVE")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]BOLD

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import BOLD

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.BOLD")
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]BOLDDOTTED
から
WDF-)[Writer]BOLDWAVE
まで、フォント名が異なるだけなので省略

WDF-)[Writer]下線色

WriterFont.py
import uno

from com.sun.star.awt.FontUnderline import DOUBLE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=20
    oTextCursor.CharHeightAsian=20
    oTextCursor.CharUnderline = uno.getConstantByName("com.sun.star.awt.FontUnderline.DOUBLE")
    oTextCursor.CharUnderlineColor = 2918503        #Color of the Underline of Font
    oTextCursor.CharUnderlineHasColor = True        #NG : true
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有
    #On Error Goto関係は省略

WDF-)[Writer]下線と下線色
からの
' 下線
Dim oProp(2) as new com.sun.star.beans.PropertyValue
oProp(0).Name = "Underline.LineStyle"
oProp(0).Value = com.sun.star.awt.FontUnderline.SINGLE ' = 1
oProp(1).Name = "Underline.HasColor"
oProp(1).Value = true
oProp(2).Name = "Underline.Color"
oProp(2).Value = &HFF0000 ' Red
oDispatcher.executeDispatch(oFrame, ".uno:Underli

のコードをどのように置換えて良いのかわからない〜
setPropertyValueのような気がするが、うまくいかない

良さそうな記事を発見、下記を参考にして挑戦していますが、・・・

pythonで構造体的なものを
https://sak12.blogspot.jp/2013/09/python.html

WDF-)[Writer]影付き文字(1)

WriterFont.py
import uno

from com.sun.star.awt.FontWeight import ULTRABOLD

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharFontName = "Arial"
    oTextCursor.CharFontNameAsian = "Arial"
    oTextCursor.CharHeight=20
    oTextCursor.CharHeightAsian=20
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oTextCursor.CharShadowed = False
    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有

    oDisp = "\n"
    oText.insertString(oText.getEnd(), oDisp, False)

    oTextCursor.CharFontName = "Arial"
    oTextCursor.CharFontNameAsian = "Arial"
    oTextCursor.CharHeight = 20
    oTextCursor.CharHeightAsian = 20
    oTextCursor.CharWeight = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oTextCursor.CharWeightAsian = uno.getConstantByName("com.sun.star.awt.FontWeight.ULTRABOLD")
    oTextCursor.CharShadowed = True

    oDisp = "AbcDe12345これはテストです"
    oText.insertString(oText.getEnd(), oDisp, False)
    #On Error Goto関係は省略

WDF-)[Writer]取り消し線1

WriterFont.py
import uno

from com.sun.star.awt.FontStrikeout import SINGLE

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharStrikeout = uno.getConstantByName("com.sun.star.awt.FontStrikeout.SINGLE")

    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有

WDF-)[Writer]取り消し線4

WriterFont.py
import uno

from com.sun.star.awt.FontStrikeout import SLASH

def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharStrikeout = uno.getConstantByName("com.sun.star.awt.FontStrikeout.SLASH")

    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有

WDF-)[Writer]取り消し線(CrossedOut)[1]

WriterFont.py
def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharFontName = "Arial"
    oTextCursor.CharFontNameAsian = "Arial"
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharCrossedOut = True

    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有

WDF-)[Writer]点滅

WriterFont.py
def WriterFont( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oTextCursor = oText.createTextCursor()
    oTextCursor.CharFontName = "Arial"
    oTextCursor.CharFontNameAsian = "Arial"
    oTextCursor.CharHeight=40
    oTextCursor.CharHeightAsian=40
    oTextCursor.CharFlash = True

    oText.String = "ABCDEFG1234これはテストです"    #Writerは先に書式設定する必要有

[ Text ]

WD-)[Writer]文字入力

WriterText.py
def WriterText( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oSText = "[ Text Start ] " + "\n"
    oEText = "\n" + "[ Text End ] "
    oText.insertString(oText.getStart(), oSText , False)  #文頭
    oText.insertString(oText.getEnd(), oEText, False)     #文末

WD-)[Writer]Documentの最初に文字入力

TextStart.py
def WriterTextStart( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oCur = oText.createTextCursor()

    oCur.gotoStart(False)
    oCur.setString("「Documentの最初に追加した文です。」")

WD-)[Writer]Documentの最後に文字入力

TextEnd.py
def WriterTextEnd( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oCur = oText.createTextCursor()

    oCur.gotoEnd(False)
    oCur.setString("「Documentの最初に追加した文です。」")

WD-)[Writer]Documentの最初のParagraphのStart位置に文字挿入

TextParaStart.py
def WriterTextParaStart( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oCur = oText.createTextCursor()

    oCur.gotoStartOfParagraph(False)
    oCur.setString("「Macroにて追加した文です。」")

WD-)[Writer]Documentの最初のParagraphのEndt位置に文字挿入

TextParaEnd.py
def WriterTextParaEnd( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oCur = oText.createTextCursor()

    oCur.gotoEndOfParagraph(False)
    oCur.setString("「Macroにて追加した文です。」")

WD-)[Writer]Next Paragraph(2番目)のStart位置に文字入力

TextPara2Start.py
def WriterTextPara2Start( ):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oText = oDoc.getText()
    oCur = oText.createTextCursor()

    oCur.gotoNextParagraph(False)
    oCur.setString("「Macroにて追加した文です。」")

Apache OpenOffice
文書ドキュメントの編集
https://wiki.openoffice.org/wiki/JA/Documentation/BASIC_Guide/Editing_Text_Documents

Transfer from Basic to Python
https://wiki.openoffice.org/wiki/Python/Transfer_from_Basic_to_Python

4
1
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
4
1