xbot.word.Document

この記事の内容
  1. メソッド
  2. save()
  3. save_as()
  4. close()
  5. export_to_pdf()
  6. get_selection()
  7. insert_hyperlink()
  8. insert_table()
  9. read_table_from_document()
  10. locate_cursor_to_bookmark()

以下のURL、ページタイトル、ウィンドウ名、要素名は説明用の例です。実際に操作する環境の値に置き換えてください。要素ライブラリに登録する名前も、コード内の指定と一致させてください。

Wordのワークブックに対して、表の挿入、表の取得、PDFへのエクスポートなどの処理を行います。

メソッド

save()

ワークブックをWordファイルとして保存します。

save(self)

入力パラメータ

なし

戻り値

なし

例1

from xbot import word


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

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> ファイルを保存します

save_as()

ワークブックに名前を付けてWordファイルとして保存します。

save_as(self, filename)

入力パラメータ

filename:名前を付けて保存する先のパスです。

戻り値

なし

例1

from xbot import word


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

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> "D:\test1.docx" として名前を付けて保存します

close()

ワークブックを閉じます。

close(self)

入力パラメータ

なし

戻り値

なし

例1

from xbot import word


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

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> Word文書を閉じます

export_to_pdf()

Word文書をPDFで保存します。

export_to_pdf(self, pdf_name, export_range = 0, page_from = 0, page_to = 0, overwrite = True)

入力パラメータ

pdf_name:保存するPDFファイルの絶対パスです。

export_range:エクスポートする範囲です。0はすべてのページ、1は現在の選択範囲、2は現在のページ、3は指定のページを表します。

page_from:指定のページをエクスポートする際の開始ページです。export_range=3の場合に有効です。

page_to:指定のページをエクスポートする際の終了ページです。export_range=3の場合に有効です。

override:エクスポート先のPDFファイルがすでに存在する場合に、上書きするかどうかを指定します。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.export_to_pdf('D:\\test.pdf', 0)

この例の実行内容: Wordファイル "D:\test.docx" を開きます --> Wordファイル全体を "D:\test.pdf" にエクスポートします

get_selection()

Wordの現在の選択範囲を取得します。

get_selection(self)

入力パラメータ

なし

戻り値

BaseSelection:Wordの選択範囲オブジェクトです。

例1

from xbot import word


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

この例の実行内容: Wordファイル'D:\test.docx'を開きます --> 現在の選択範囲オブジェクトを取得します

ハイパーリンクを挿入します。

insert_hyperlink(self, text, hyperlink_url, newline = False)

入力パラメータ

text:ハイパーリンクの表示文字列です。

hyperlink_url:アドレスです。

newline:ハイパーリンクを挿入する前に改行するかどうかを指定します。Trueは改行する、Falseは改行しないことを表します。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.insert_hyperlink('検索ページ', 'example.com', True)

この例の実行内容: Wordファイル'D:\test.docx'を開きます --> ハイパーリンク'検索ページ'を挿入します

insert_table()

Word文書に表を挿入します。

insert_table(self, table_data, table_data_format, grid = True, newline = False)

入力パラメータ

table_data:表データです。二次元リスト形式を使用します。

table_data_format:表の書式の内容です。クライアントから取得できます。

grid:表の罫線を表示するかどうかを指定します。Trueは表示する、Falseは表示しないことを表します。

newline:ハイパーリンクを挿入する前に改行するかどうかを指定します。Trueは改行する、Falseは改行しないことを表します。

戻り値

なし

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.insert_table([['1','2','3'],['4','5','6']], '{"font": {"name": "宋体","style": "常规","size": 11,"color": 0,"strikethrough": false,"subscript": false,"superscript": false,"bold": false,"italic": false},"alignment": {"horizontalAlignment": 1,"verticalAlignment": 1}}')

この例の実行内容: Wordファイル'D:\test.docx'を開きます --> データテーブルを挿入します

read_table_from_document()

Word文書の表を取得します。

read_table_from_document(self, find_way, table_index, table_content)

入力パラメータ

find_way:表の特定方法です。'index'は位置に基づく検索、'content'は内容に基づく検索を表します。

table_index:表の位置です。find_way='index'の場合に有効です。

table_content:表の内容です。find_way='content'の場合に有効です。

戻り値

List[List]:表の内容のテキストを格納した二次元リストです。

例1

from xbot import word


def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    table_data = document.read_table_from_document('index', 1)
    print(table_data)

この例の実行内容: Wordファイル'D:\test.docx'を開きます --> 1番目のデータテーブルを取得します

locate_cursor_to_bookmark()

カーソルをブックマークの位置に移動します。

locate_cursor_to_bookmark(self, bookmark_name, bookmark_cursor_position = 'after_bookmark')

入力パラメータ

bookmark_name:ブックマーク名です。

bookmark_cursor_position:ブックマークに対するカーソル位置です。'before_bookmark'はブックマークの直前、'after_bookmark'はブックマークの直後を表します。

戻り値

なし

例1

from xbot import word
def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.locate_cursor_to_bookmark('书签1')

この例の実行内容: Wordファイル'D:\test.docx'を開きます --> カーソルをブックマーク'书签1'の直後に移動します

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