บทความนี้อธิบาย วิธีลบหน้าว่างใน Word โดยใช้ Python โดยมีรายละเอียดการตั้งค่าสภาพแวดล้อม รายการขั้นตอน และโค้ดตัวอย่างที่รันได้ซึ่งแสดง วิธีลบหน้าว่างใน Word โดยใช้ Python คุณสามารถใช้โค้ดตัวอย่างเพื่อดำเนินงานได้ อย่างไรก็ตาม คุณสามารถปรับแต่งโค้ดตัวอย่างได้โดยเพิ่มเงื่อนไขต่างๆ หากจำเป็น
ขั้นตอนในการลบหน้าว่างใน Word โดยใช้ Python
- ตั้งค่าสภาพแวดล้อมการพัฒนาเพื่อใช้ Aspose.Words สำหรับ Python ผ่าน .NET เพื่อลบหน้าว่าง
- โหลดไฟล์ Word ต้นฉบับลงในออบเจ็กต์ Document ที่มีหน้าว่างอยู่ในนั้น
- วนซ้ำทุกหน้าในเอกสารและตรวจสอบเนื้อหาของแต่ละส่วน
- ตรวจสอบการมีอยู่ของข้อความและรูปภาพ และเตรียมรายการหน้าว่าง
- สร้างเอกสาร Word เปล่าใหม่
- Append หน้าทั้งหมดที่มีเนื้อหาบางส่วนจากไฟล์ Word ต้นฉบับโดยใช้รายการหน้าว่าง
- บันทึกไฟล์ Word ที่เป็นผลลัพธ์โดยไม่มีหน้าว่าง
ขั้นตอนเหล่านี้อธิบายกระบวนการ วิธีลบหน้า Word ว่างโดยใช้ Python ในกระบวนการนี้ ไฟล์ Word ต้นฉบับจะถูกโหลด และทุกส่วนในแต่ละหน้าจะถูกตรวจสอบว่ามีข้อความหรือรูปภาพเพื่อเตรียมรายการหน้าว่างหรือไม่ ในท้ายที่สุด เอกสารใหม่จะถูกสร้างขึ้น และหน้าทั้งหมดนอกเหนือจากหน้าว่างจะถูกเพิ่มลงในเอกสารเปล่าใหม่นี้
รหัสเพื่อลบหน้าว่างใน Word โดยใช้ Python
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
# Load the license | |
wordLic = aw.License() | |
wordLic.set_license("License.lic") | |
# Load the Word file having blank pages in it | |
originalDocWithFewBlankPages = aw.Document("input.docx") | |
# Declare an array for blank page numbers | |
listOfBlankPageNumbers = [] | |
listOfBlankPageNumbers.append(-1) | |
# Get total pages in the souce Word file | |
totalPagesInOriginalDoc = originalDocWithFewBlankPages.page_count | |
for iCount in range(0, totalPagesInOriginalDoc): # This will loop for page count | |
# Extract each page one by one | |
DocWithOnePage = originalDocWithFewBlankPages.extract_pages(iCount, 1) | |
shapesCounter = 0 | |
pageText = "" | |
# Check text and images in each section | |
for docSection in DocWithOnePage.sections: | |
docSection = docSection.as_section() | |
pageText = pageText + docSection.body.to_string(aw.SaveFormat.TEXT) | |
shapesCounter += docSection.body.get_child_nodes(aw.NodeType.SHAPE, True).count | |
# Check if no content is there | |
if (not pageText) and shapesCounter == 0: | |
# Store the index of blank page numbers | |
listOfBlankPageNumbers.append(iCount) | |
listOfBlankPageNumbers.append(totalPagesInOriginalDoc) | |
# Create a single page document with default forrmat | |
nonEmptyDocument = originalDocWithFewBlankPages.clone(False).as_document() | |
# Clean the document | |
nonEmptyDocument.remove_all_children() | |
# Append the pages with content to the new document | |
for iCount in range(1, len(listOfBlankPageNumbers)): | |
index = listOfBlankPageNumbers[iCount - 1] + 1 | |
count = listOfBlankPageNumbers[iCount] - index | |
if count > 0: | |
nonEmptyDocument.append_document( | |
originalDocWithFewBlankPages.extract_pages(index, count), | |
aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
# Save the document having some content in it | |
nonEmptyDocument.save("NonEmptyPages.docx") | |
print ("Blank pages deleted successfully") |
โค้ดตัวอย่างนี้สาธิต วิธีลบหน้าว่างจาก Word โดยใช้ Python ในโค้ดตัวอย่างนี้ ข้อความเนื้อหาและประเภทโหนด SHAPE ได้รับการทดสอบสำหรับแต่ละส่วน อย่างไรก็ตาม คุณอาจใช้ประเภทอื่น เช่น TABLE, COMMENT, FORM_FIELD, SMART_TAG ฯลฯ เพื่อระบุหน้าว่าง เมธอด append_document() ใช้สำหรับการเพิ่มหน้าลงในไฟล์ Word ใหม่ ในขณะที่มีวิธีการโอเวอร์โหลดต่างๆ มากมาย ทำให้เกิดอาร์กิวเมนต์ที่แตกต่างกันสำหรับการปรับแต่งกระบวนการแทรกหน้า
บทความนี้สอนเรา วิธีลบหน้าว่างใน Word โดยใช้ Python หากคุณต้องการใส่หมายเลขหน้าในไฟล์ Word โปรดดูบทความเกี่ยวกับ วิธีใส่หมายเลขหน้าใน Word โดยใช้ Python