วิธีเปลี่ยนคุณสมบัติของเอกสาร Word ใน Python

บทความนี้ให้คำแนะนำเกี่ยวกับ วิธีเปลี่ยนคุณสมบัติของเอกสาร Word ใน Python มีส่วนผสมทั้งหมดในการตั้งค่าสภาพแวดล้อมสำหรับการพัฒนาแอปพลิเคชันและรายการขั้นตอนที่ต้องปฏิบัติตามพร้อมกับโค้ดตัวอย่างที่เรียกใช้ได้เพื่อ เปลี่ยนข้อมูลเมตาของ Word ใน Python คุณจะได้เรียนรู้ตัวเลือกในการเข้าถึงคุณสมบัติที่เลือกโดยใช้ดัชนีหรือชื่อคุณสมบัติตามข้อกำหนด

ขั้นตอนในการแก้ไขข้อมูลเมตาของ Word ใน Python

  1. ตั้งค่า IDE ให้ใช้ Aspose.Words สำหรับ Python ผ่าน .NET เพื่ออัปเดตข้อมูลเมตา
  2. โหลดไฟล์ต้นฉบับโดยใช้อ็อบเจ็กต์ Document และเข้าถึงคอลเล็กชันคุณสมบัติที่กำหนดเอง
  3. ตรวจสอบว่ามีคุณสมบัติเป้าหมายอยู่หรือไม่ จากนั้นเข้าถึงคุณสมบัติและตั้งค่าใหม่
  4. เข้าถึง built-in properties และอัปเดตค่าที่เกี่ยวข้อง
  5. บันทึกไฟล์ Word ที่เป็นผลลัพธ์ด้วยคุณสมบัติใหม่

ขั้นตอนเหล่านี้สรุปกระบวนการเพื่อ แก้ไขคุณสมบัติเอกสารใน Word ใน python กระบวนการเริ่มต้นด้วยการโหลดเอกสารต้นทางและเข้าถึงคุณสมบัติแบบกำหนดเองโดยใช้คอลเลกชัน custom_document_properties ซึ่งเข้าถึงคุณสมบัติแต่ละรายการโดยใช้ดัชนีรายการ ในทำนองเดียวกัน คุณสมบัติในตัวสามารถเข้าถึงได้โดยใช้คอลเล็กชัน built_in_document_properties และแก้ไข

รหัสเพื่อแก้ไขข้อมูลเมตาของเอกสาร Word ใน Python

import aspose.words as aw
import aspose.pydrawing as drawing
from datetime import datetime, date
# Load the license
wordLic = aw.License()
wordLic.set_license("Aspose.Total.lic")
# Load the original document
doc = aw.Document("SampleProps.doc")
# Get custom properties
custProps = doc.custom_document_properties
if custProps.__getitem__(custProps.index_of("Authorized")).value != None:
# Set properties
custProps.__getitem__(custProps.index_of("Authorized By")).value = "John"
custProps.__getitem__(custProps.index_of("Authorized Date")).value = date(2023, 6, 12)
custProps.__getitem__(custProps.index_of("Authorized Revision")).value = 200
custProps.__getitem__(custProps.index_of("Authorized Amount")).value = 400
# Get built-in properties
documentProperties = doc.built_in_document_properties
# Set new properties
documentProperties.__getitem__(documentProperties.index_of("Subject")).value = "Test Subject"
documentProperties.__getitem__(documentProperties.index_of("Manager")).value = "Test Manager"
documentProperties.__getitem__(documentProperties.index_of("Company")).value = "Test Company"
# Save the output
doc.save("Output.doc");
print ("Word file metadata is updated")

ส่วนโค้ดนี้แสดงขั้นตอนการพัฒนา ตัวเปลี่ยนข้อมูลเมตาของ Word เมธอด getitem() ต้องการดัชนีของคุณสมบัติที่จะเข้าถึง สำหรับจุดประสงค์นี้ เมธอด index_of() ต้องการชื่อของคุณสมบัติ อย่างไรก็ตาม หากคุณทราบดัชนีของคุณสมบัติเป้าหมาย ให้ใช้เมธอด getitem()

ภายในบทความนี้ เราได้เจาะลึกกระบวนการเปลี่ยนแปลงข้อมูลเมตา หากคุณต้องการเรียนรู้ขั้นตอนการแทรกบุ๊กมาร์กในไฟล์ Word โปรดดูบทความใน วิธีแทรกบุ๊กมาร์กใน Word โดยใช้ Python

 ไทย