xbot.word.Selection

この記事の内容
  1. メソッド
  2. read_text()
  3. write_text()
  4. insert_newline()
  5. insert_picture()
  6. replace_text()
  7. move_cursor()
  8. locate_cursor_to_document()
  9. locate_cursor_to_text()

Wordファイルの内容に対して、テキストの読み取り、テキストの書き込み、テキストの置換、画像の挿入、改行など、さまざまな処理を行います。

メソッド

read_text()

現在の選択範囲のテキスト内容を読み取ります。

read_text(self)

入力パラメータ

なし

戻り値

any:テキスト文字列を返します。

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    text = selection.read_text()

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Word内の選択範囲のテキストを読み取ります

write_text()

Wordのカーソル位置にテキストを書き込みます。

write_text(self, text, text_format, newline = False)

入力パラメータ

text:書き込むテキストの内容です。

text_format:書き込むテキストの文字書式です。

newline:テキストを書き込む前に改行するかどうかを指定します。デフォルト値はFalse(改行しない)です。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.write_text('私は大潤発で10年間魚をさばいてきました。私の心はとうの昔に、魚をさばく包丁のように冷たくなっています','{"font": {"name": "宋体","size": 11,"color": 0,"subscript": false,"superscript": false,"bold": false,"italic": false}}')

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Wordにテキストを書き込みます

insert_newline()

Wordに改行を挿入します。

insert_newline(self)

入力パラメータ

なし

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.insert_newline()

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Wordに改行を挿入します

insert_picture()

Wordに画像を挿入します。

insert_picture(self, image_source, image_path, image_scale, newline = False)

入力パラメータ

image_source:画像の取得元です。'file'は画像ファイルの挿入を表します。'clipboard'はクリップボードを表し、クリップボード内の画像データを直接貼り付けます。

image_path:挿入する画像のパスです。

image_scale:画像の拡大・縮小率(パーセント)です。

newline:テキストを書き込む前に改行するかどうかを指定します。デフォルト値はFalse(改行しない)です。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.insert_picture('file','D:\\test.jpg', 100)

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Wordに画像を挿入します。拡大・縮小率は100%です

replace_text()

Word内のテキストを置換します。

replace_text(self, search_text, replace_text, replace_all = True, case_sensitive = False)

入力パラメータ

search_text:検索するテキストです。

replace_text:置換後のテキストです。

replace_all:すべての文字列を置換するかどうかを指定します。デフォルト値はTrue(すべての文字列を置換)です。

case_sensitive:大文字・小文字を区別するかどうかを指定します。デフォルト値はFalse(区別しない)です。

戻り値

int:テキストを置換した回数を返します。

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.replace_text('1', '2')

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Word内で、1 を 2 に置換します

move_cursor()

Word内のカーソルを移動します。

move_cursor(self, move_direction, move_count, press_shift = False)

入力パラメータ

move_direction:移動方向です。'move_left'は左方向、'move_up'は上方向、'move_right'は下方向、'move_down'は下方向を表します。

move_count:移動する長さを単位数で指定します。

press_shift:移動時にShiftキーを押下するかどうかを指定します。デフォルト値はFalseです。Trueの場合、移動した範囲のテキストがすべて選択されます。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.move_cursor('move_right', 5)

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Wordのテキスト内で右方向に5単位分移動します

locate_cursor_to_document()

カーソルをWord文書の先頭や末尾に移動します。

locate_cursor_to_document(self, document_cursor_position = 'end_document')

入力パラメータ

document_cursor_position:'begin_document'はテキストの先頭、'end_document'はテキストの末尾を表します。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.locate_cursor_to_document('begin_document')

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Word内でカーソルを文書の末尾に移動します

locate_cursor_to_text()

カーソルをWord内の指定したテキストの位置に移動します。

locate_cursor_to_text(self, text_content, text_index = 1, text_cursor_position = 'after_text', from_start = True)

入力パラメータ

text_content:カーソルの移動先となるテキストの内容です。

text_index:起点から検索したテキストの位置です。

text_cursor_position:テキストに対するカーソル位置です。'before_text'は文字列の直前への移動、'after_text'は文字列の直後への移動を表します。

from_start:先頭から検索するかどうかを指定します。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()
    selection.locate_cursor_to_text('我', 2)

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> 範囲を選択します --> Word内で'我'という文字の位置から後方へ2文字分探し、カーソルをその位置に移動します

APIリファレンスの記事一覧ページの先頭へ ↑