Python
 Computer >> コンピューター >  >> プログラミング >> Python

Microsoft Wordの段落を作成し、Pythonで画像を挿入する方法は?


はじめに...

データエンジニアリングのスペシャリストである私は、MicrosoftWordでテスターからテスト結果を受け取ることがよくあります。はぁ!スクリーンショットや非常に大きな段落をキャプチャすることで、Word文書に非常に多くの情報を投入しました。

先日、テストチームから、ツールで生成されたテキストと画像(自動スクリーンショットで撮影。この記事では取り上げていません)を挿入するプログラムのサポートを依頼されました。

MS Word文書は、他の文書とは異なり、残念ながら段落で機能するため、ページの概念がありません。したがって、文書を適切に分割するには、区切り文字とセクションを使用する必要があります。

その方法..

1.先に進み、python-docxをインストールします。

import docx

# create a new couments
WordDocx = docx.Document()

# My paragraph.
Paragraph = WordDocx.add_paragraph('1. Hello World, Some Sample Text Here...')
run = Paragraph.add_run()

# paragraph with a line break
run.add_break(docx.text.run.WD_BREAK.LINE)

# Add more
Paragraph.add_run('2. I have just written my 2nd line and I can write more..')

# Finally savind the document.
WordDocx.save('My_Amazing_WordDoc.docx')

2.さて、内容に問題がないか確認してみましょう。あなたはプログラマーなので、プログラムで行います。

doc = docx.Document('My_Amazing_WordDoc.docx')
print(f"output \n *** Document has {len(doc.paragraphs)} - paragraphs")
for paragraph_number, paragraph in enumerate(doc.paragraphs):
if paragraph.text:
print(f"\n {paragraph.text}")

出力

*** Document has 1 - paragraphs

1. Hello World, Some Sample Text Here...
2. I have just written my 2nd line and I can write more..

3.次に、ドキュメントに画像を追加します。したがって、最初に画像を探す必要があります。 unsplash.comから、著作権の問題がない画像をダウンロードしました。インターネットからダウンロードするものは、細心の注意を払って実行するようにしてください。

Unsplashには、著作権のない画像があり、あらゆる目的に使用できます。彼らの作品に感謝します。

画像をダウンロードして、ドキュメントに追加されるTree.imgという名前を付けました。

import requests
from docx.shared import Cm

# Download the image from Github
response = requests.get("https://raw.githubusercontent.com/sasankac/TestDataSet/master/Tree.jpg")
image = open("Tree.jpg", "wb")
image.write(response.content)
image.close()

# add the image
image_to_add = doc.add_picture("Tree.jpg")
print(f"output \n *** MY Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

出力

*** MY Image has width = 43891200 and Height as - 65836800

4.画像が大きすぎるため、画像を適切に拡大縮小する必要があります。幅と高さのパラメータを使用できます。

image_to_add.width = Cm(10)
image_to_add.height = Cm(10)
print(f" *** My New dimensions Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

# finally save the document
doc.save('report.docx')


*** My New dimensions Image has width = 3600000 and Height as - 3600000

5.ドキュメントを開くと、画像とテキストが追加されているのがわかります。

6.すべてをまとめます。

import requests
from docx.shared import Cm

# Download the image from Github
response = requests.get("https://raw.githubusercontent.com/sasankac/TestDataSet/master/Tree.jpg")
image = open("Tree.jpg", "wb")
image.write(response.content)
image.close()

# add the image
image_to_add = doc.add_picture("Tree.jpg")
print(f"output \n *** MY Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

image_to_add.width = Cm(10)
image_to_add.height = Cm(10)
print(f" *** My New dimensions Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

# finally save the document
doc.save('report.docx')
を保存します

出力

*** MY Image has width = 43891200 and Height as - 65836800
*** My New dimensions Image has width = 3600000 and Height as - 3600000

Microsoft Wordの段落を作成し、Pythonで画像を挿入する方法は?


  1. MicrosoftWordで封筒を作成して印刷する方法

    Microsoft Officeアプリケーションは、多くのユニークでユーザーフレンドリーな機能を備えて設計されています。これらの機能により、ユーザーは物事を成し遂げるだけでなく、多くのことを同時に行うことができます。たとえば、プロの封筒を作成できます MicrosoftWordアプリケーションを使用します。 MicrosoftWordで単一の封筒を作成して印刷する プロセスに気付いていない場合は、MicrosoftWordを使用して封筒を作成する手順を説明します。作成したら、封筒を自分のプリンターで印刷するか、商用プリンターに送信して印刷することができます。完全な手順は2つのステップにまとめ

  2. Microsoft Word で定型句を作成して使用する方法

    ワード プロセッサは、Microsoft が最初に MS-DOS 用の Microsoft Word をリリースした 1980 年代初頭から長い道のりを歩んできました。その画期的な機能は、マウスで使用するように設計されていることです。現在のバージョンの Microsoft Word には、Microsoft Word の AutoText 機能を使用してコンテンツをより迅速に作成し、間違いを減らすなど、ユーザーが当時は想像もできなかった機能が含まれています。 Word を頻繁に使用する場合、頻繁に使用する語句、テキスト ブロック、またはグラフィックスがある可能性があります。 Word のオ