らんだむな記憶

blogというものを体験してみようか!的なー

OpenOfficeのpythonマクロ(2)

# -*- coding: utf-8 -*-

FONT_NAME_ASIAN = u"源ノ明朝 SemiBold"

# https://wiki.openoffice.org/wiki/JA/Documentation/BASIC_Guide/Structure_of_Text_Documents
def change_text_font(text, font_name):
    enumeration = text.createEnumeration()
    while enumeration.hasMoreElements():
        text_element = enumeration.nextElement()
        if not text_element.supportsService("com.sun.star.text.Paragraph"):
            continue
        enumeration2 = text_element.createEnumeration()
        while enumeration2.hasMoreElements():
            text_portion = enumeration2.nextElement()
            # 英数部分の置換
            text_portion.CharFontName = font_name
            # 和文部分の置換
            text_portion.CharFontNameAsian = font_name

def change_document_font(dummy=None):
    doc = XSCRIPTCONTEXT.getDocument()
    text = doc.getText()
    change_text_font(text, FONT_NAME)

    enumeration = doc.getTextFrames().createEnumeration()
    while enumeration.hasMoreElements():
        text = enumeration.nextElement()
        change_text_font(text, FONT_NAME)

# マクロの公開
g_exportedScripts = (
    change_document_font,
)

のような感じでドキュメント内のフォントをざっくり置換できそうだ・・・。疲れた・・・
API が分かってきたので、JScriptディレクトリ内の全 .odt を一括で処理だー!とかできそうに思えてくるが、もうそんな気力がない・・・。