如何在 Python 中创建 Microsoft Word 段落并插入图像?


简介...

作为一名数据工程专家,我经常收到测试人员在 Microsoft Word 中的测试结果。唉!他们把很多信息都放在 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. 我们需要正确缩放图像,因为我的图像太大。我们可以使用 width 和 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 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

更新时间: 2020年11月10日

390 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.