แนะนำตัว...
การเป็นผู้เชี่ยวชาญด้าน Data Engineering ฉันมักจะได้รับผลการทดสอบจากผู้ทดสอบใน Microsoft 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
