0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでWord文書にテキスト ボックスを挿入・削除する方法

Posted at

オフィス業務の自動化ニーズが高まる中、Word 文書内のテキスト ボックスをプログラムで動的に管理することは、業務効率化の鍵となる技術のひとつです。テキスト ボックスは、文字列や画像などの情報を自由に配置できる柔軟なコンテンツ コンテナーであり、本文のレイアウトとは独立して位置やスタイルを調整できます。しかし、手作業での編集は繰り返しが多く、大量処理にも向いていません。

Python スクリプトを活用することで、テキスト ボックスの一括挿入・削除や、高度なレイアウト設計とデータの動的な連携が可能になります。たとえば、レポートのテンプレート生成、企業文書の書式統一、あるいはインタラクティブな文書構築などに応用できます。

本記事では、Python を使用して Word 文書にテキスト ボックスを挿入・削除する方法を紹介します。

この記事で使用するライブラリは Free Spire.Doc for Python です。PyPI から以下のコマンドでインストールできます:
pip install spire.doc.free

PythonでWord文書にテキストボックスを挿入する方法

Spire.Doc の Document クラスを使って Word 文書を新規作成または読み込み、Paragraph.AppendTextBox(width: float, height: float) メソッドで任意サイズのテキスト ボックスを段落内に追加できます。テキスト ボックスの書式は TextBox.Format プロパティで設定でき、文字列や画像などの要素を中に挿入できます。

操作手順:

  1. Document のインスタンスを作成し、Word 文書を新規作成します。
  2. Document.AddSection() メソッドでセクションを追加し、ページ設定を行います。
  3. Section.AddParagraph() メソッドで段落を追加し、任意の内容を挿入します。
  4. Paragraph.AppendTextBox() メソッドで、指定サイズのテキスト ボックスを挿入します。
  5. TextBox.Format プロパティを使って、テキスト ボックスの折り返しの種類や位置、枠線、塗りつぶしの色などを設定します。
  6. TextBox.Body.AddParagraph() メソッドで、テキスト ボックス内に段落を追加します。
  7. テキスト ボックス内の段落に文字列や画像を追加し、必要に応じて書式を設定します。
  8. Document.SaveToFile() メソッドで文書を保存します。

コード例:

from spire.doc import Document, VerticalOrigin, TextWrappingStyle, HorizontalOrigin, BackgroundType, Color, TextBoxLineStyle, LineDashing, HorizontalAlignment, FileFormat

# ドキュメントオブジェクトを作成します
document = Document()

# セクションを追加します
section = document.AddSection()
section.PageSetup.Margins.Top = 50
section.PageSetup.Margins.Bottom = 50

# 段落を追加します
main_paragraph = section.AddParagraph()
text_range = main_paragraph.AppendText("以下はテキストボックスのサンプルです:\r\n")
text_range.CharacterFormat.FontName = "Yu Gothic UI"
text_range.CharacterFormat.FontSize = 14

# 段落内にテキストボックスを追加します(サイズを設定)
textbox = main_paragraph.AppendTextBox(300, 220)

# テキストボックスの位置と文字列の折り返しスタイルを設定します
textbox.Format.HorizontalOrigin = HorizontalOrigin.Page
textbox.Format.HorizontalPosition = 50
textbox.Format.VerticalOrigin = VerticalOrigin.Page
textbox.Format.VerticalPosition = 80
textbox.Format.TextWrap = TextWrappingStyle.InFrontOfText

# グラデーションの背景塗りを設定します
textbox.Format.FillEfects.Type = BackgroundType.Gradient
textbox.Format.FillEfects.Gradient.Color1 = Color.get_BlueViolet()
textbox.Format.FillEfects.Gradient.Color2 = Color.get_LightSkyBlue()
textbox.Format.FillEfects.Gradient.Angle = 45

# 立体的な枠線スタイルを設定します
textbox.Format.LineColor = Color.get_DarkBlue()
textbox.Format.LineStyle = TextBoxLineStyle.ThickThin
textbox.Format.LineWidth = 3.5
textbox.Format.LineDashing = LineDashing.DashDot

# テキストボックス内にアイコン付きの見出し段落を追加します
header_paragraph = textbox.Body.AddParagraph()
icon = header_paragraph.AppendPicture("alert.png")
icon.Width = 20
icon.Height = 20

title_text = header_paragraph.AppendText(" 重要なお知らせ")
title_text.CharacterFormat.FontName = "Yu Gothic UI"
title_text.CharacterFormat.FontSize = 14
title_text.CharacterFormat.TextColor = Color.get_Gold()

# 区切り線を追加します
separator = textbox.Body.AddParagraph()
separator.Format.Borders.Top.Color = Color.get_Gold()
separator.Format.Borders.Top.LineWidth = 2
separator.Format.Borders.Top.Space = 5

# 画像とテキストを組み合わせた内容を追加します
content_paragraph = textbox.Body.AddParagraph()
image = content_paragraph.AppendPicture("document.png")
image.Width = 60
image.Height = 60
image.TextWrappingStyle = TextWrappingStyle.Through

text_content = content_paragraph.AppendText("このお知らせには重要な更新内容が含まれています。よくお読みください。さらに操作が必要な場合は、ナレッジベースを参照するか、テクニカルサポートまでご連絡ください。")
text_content.CharacterFormat.FontName = "Yu Mincho Demibold"
text_content.CharacterFormat.FontSize = 12
text_content.CharacterFormat.TextColor = Color.get_WhiteSmoke()

# 回転したテキスト装飾を追加します
rotated_paragraph = textbox.Body.AddParagraph()
rotated_text = rotated_paragraph.AppendText("機密文書")
rotated_text.CharacterFormat.FontName = "Yu Mincho Demibold"
rotated_text.CharacterFormat.FontSize = 24
rotated_text.CharacterFormat.TextColor = Color.FromArgb(100, 255, 255, 255)
rotated_paragraph.Format.Rotation = 30  # 30度回転
rotated_paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right

# ドキュメントを保存します
document.SaveToFile("output/TextBox.docx", FileFormat.Docx2019)
document.Close()

出力結果:
PythonでWord文書にテキストボックスを挿入する方法

PythonでWord文書からテキストボックスを削除する方法

Spire.Doc では、Document.TextBoxes プロパティから文書内のすべてのテキスト ボックスを取得できます。そのうえで、RemoveAt() メソッドを使って指定のテキスト ボックスを削除したり、Clear() メソッドですべてのテキスト ボックスを一括削除したりできます。

操作手順:

  1. Document オブジェクトを作成し、Document.LoadFromFile() メソッドで Word 文書を読み込みます。
  2. Document.TextBoxes プロパティで文書内のすべてのテキスト ボックスを取得します。
  3. TextBoxCollection.RemoveAt() メソッドで指定のテキスト ボックスを削除、または TextBoxCollection.Clear() メソッドですべてのテキスト ボックスを削除します。
  4. Document.SaveToFile() メソッドで文書を保存します。

コード例:

from spire.doc import Document

# Document オブジェクトを作成します
document = Document()

# Word 文書を読み込みます
document.LoadFromFile("output/TextBox.docx")

# 指定したテキストボックスを削除します
document.TextBoxes.RemoveAt(0)

# すべてのテキストボックスを削除します
document.TextBoxes.Clear()

# 文書を保存します
document.SaveToFile("output/RemoveTextBox.docx")
document.Close()

出力結果:
PythonでWord文書からテキストボックスを削除する方法


本記事では、Python を用いて Word 文書におけるテキスト ボックスの挿入および削除を行う方法について、操作手順とコード例を交えて解説しました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?