ความแตกต่างของเวอร์ชัน Python และ .NET

Aspose.Words สำหรับ Python via .NET เป็น wrapper ของ Aspose.Words สำหรับ .NET นั่นคือสาเหตุที่ผลิตภัณฑ์ทั้งสองมีชุดคุณลักษณะที่เกือบจะเหมือนกัน อย่างไรก็ตาม มีความแตกต่างบางประการในการทำงานและความแตกต่างในฟีเจอร์และ API ซึ่งอธิบายไว้ในหน้านี้

ความแตกต่างของคุณสมบัติ

เนื่องจากกระบวนการห่อ จึงทำให้มีฟีเจอร์บางอย่างที่ไม่มีในเวอร์ชัน Python ต่อไปนี้เป็นรายการคุณลักษณะที่โดดเด่นที่สุดซึ่งปัจจุบันไม่มีในเวอร์ชัน Python

  • ยังไม่รองรับการใช้งานอินเทอร์เฟซ นั่นคือสาเหตุที่ไม่สามารถใช้การเรียกกลับเช่น IWarningCallback, IReplacingCallback, IFieldUpdatingCallback, IFieldMergingCallback เป็นต้น
  • คุณสมบัติการพิมพ์ไม่พร้อมใช้งานเนื่องจาก Aspose.Words เวอร์ชัน .NET Standard 2.0 ถูกใช้เป็นแบ็คเอนด์ของเวอร์ชัน Python ซึ่งไม่มีฟังก์ชันนี้เช่นกัน
  • มีเพียงฟังก์ชัน Mail Merge แบบธรรมดาเท่านั้นที่มีอาร์เรย์ของชื่อฟิลด์และค่าของฟิลด์เป็นแหล่งข้อมูล
  • ขณะนี้การใช้งาน DocumentVisitor ไม่สามารถทำได้จากโค้ด Python

การส่งวัตถุ Aspose.Words ใน Python

แม้ว่าการคัดแยกประเภทจะไม่ใช่เรื่องปกติสำหรับนักพัฒนา Python แต่งานบางอย่างก็ไม่สามารถทำได้สำเร็จหากไม่มีการหล่อโหนดหรือช่องเอกสารให้เป็นประเภทคอนกรีต Aspose.Words สำหรับ Python via .NET มีวิธีการพิเศษที่ช่วยให้สามารถร่ายวัตถุได้เมื่อจำเป็น

โหนดหล่อ

คลาสพื้นฐานสำหรับโหนดเอกสารทั้งหมดใน Aspose.Words DOM คือคลาส Node ตัวอย่างเช่น ส่งคืนเมธอด get_child และอินสแตนซ์ของคลาส Node แต่หากคุณต้องการแก้ไขโหนดที่ส่งคืน ในกรณีส่วนใหญ่ คุณควรส่งโหนดนั้นเป็นประเภทคอนกรีต รหัสต่อไปนี้สาธิตวิธีการเปลี่ยนสีแบบอักษรของ Run แรกในเอกสาร:

doc = aw.Document(docs_base.my_dir + "Document.docx") # Get the first Run node and cast it to Run object. run = doc.get_child(aw.NodeType.RUN, 0, True).as_run() # Make changes to the run  run.font.color = drawing.Color.red # Save the result doc.save(docs_base.artifacts_dir + "WorkingWithNode.change_run_color.docx")

อาจจำเป็นต้องมีการแคสต์เมื่อใช้วิธีการ clone:

doc = aw.Document(docs_base.my_dir + "Document.docx") clone = doc.clone().as_document() clone.save(docs_base.artifacts_dir + "CloneAndCombineDocuments.cloning_document.docx")

ดังที่คุณอาจสังเกตเห็นว่าส่วนใดในโค้ด C# คุณจะใช้ (Paragraph)node ในการแคสต์ ใน Python คุณต้องใช้วิธีการ node.as_paragraph() ในเวอร์ชัน Python ของคลาส Aspose.Words Node จะแนะนำลิงก์ของวิธี as_xxx ต่อไปนี้:

เมธอด as_xxx จะสร้าง RuntimeError ด้วยข้อความดังต่อไปนี้ หากโหนดไม่สามารถจัดเป็นประเภทที่ระบุได้:

RuntimeError: Proxy error(InvalidCastException): Unable to cast object of type ‘Aspose.Words.XXX’ to type ‘Aspose.Words.Drawing.YYY’.

สนามหล่อ

สถานการณ์เดียวกันนี้ใช้กับฟิลด์ ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแทนที่ลิงก์ไฮเปอร์ลิงก์:

doc = aw.Document(docs_base.my_dir + "Hyperlinks.docx") for field in doc.range.fields : if field.type == aw.fields.FieldType.FIELD_HYPERLINK: hyperlink = field.as_field_hyperlink() # Some hyperlinks can be local (links to bookmarks inside the document), ignore these. if hyperlink.sub_address != None : continue hyperlink.address = "https:#www.aspose.com" hyperlink.result = "Aspose - The .net & Java Component Publisher" doc.save(docs_base.artifacts_dir + "WorkingWithFields.replace_hyperlinks.docx")

ดังที่คุณอาจสังเกตเห็นว่าออบเจ็กต์ Field ยังมีชุดวิธี as_xxx อีกด้วย ซึ่งมีรายชื่ออยู่ด้านล่าง:

สไตล์การหล่อ

จำเป็นต้องมีการแคสต์เพื่อทำงานกับสไตล์ตาราง:

doc = aw.Document() builder = aw.DocumentBuilder(doc) table = builder.start_table() builder.insert_cell() builder.write("Name") builder.insert_cell() builder.write("Value") builder.end_row() builder.insert_cell() builder.insert_cell() builder.end_table() # Add a table style and modify it's properties. tableStyle = doc.styles.add(aw.StyleType.TABLE, "MyTableStyle1").as_table_style() tableStyle.borders.line_style = aw.LineStyle.DOUBLE tableStyle.borders.line_width = 1 tableStyle.left_padding = 18 tableStyle.right_padding = 18 tableStyle.top_padding = 12 tableStyle.bottom_padding = 12 table.style = tableStyle doc.save(docs_base.artifacts_dir + "WorkingWithTableStylesAndFormatting.create_table_style.docx")

กำลังหล่อการควบคุม Ole

ตัวอย่างรหัสต่อไปนี้สาธิตวิธีการอ่านคุณสมบัติการควบคุม ActiveX:

doc = aw.Document(docs_base.my_dir + "ActiveX controls.docx") properties = "" for shape in doc.get_child_nodes(aw.NodeType.SHAPE, True) : shape = shape.as_shape() if shape.ole_format == None : break oleControl = shape.ole_format.ole_control if oleControl.is_forms2_ole_control : checkBox = oleControl.as_forms2_ole_control() properties = properties + "\nCaption: " + checkBox.caption properties = properties + "\nValue: " + checkBox.value properties = properties + "\nEnabled: " + str(checkBox.enabled) properties = properties + "\nType: " + str(checkBox.type) if checkBox.child_nodes != None : properties = properties + "\nChildNodes: " + checkBox.child_nodes properties += "\n" properties = properties + "\nTotal ActiveX Controls found: " + str(doc.get_child_nodes(aw.NodeType.SHAPE, True).count) print("\n" + properties)

กำลังส่งแหล่งที่มาของแบบอักษร

คลาส FontSourceBase มีชุดวิธี as_xxx ซึ่งมีรายชื่ออยู่ด้านล่าง:

การเข้าถึงคุณสมบัติตัวสร้างดัชนี

Aspose.Words สำหรับ Python อนุญาตให้ใช้คุณสมบัติตัวสร้างดัชนีตามประเภท int เท่านั้น ใน .NET อย่างไรก็ตาม คุณสามารถใช้ประเภทอื่นได้ เช่น สตริง เพื่อเติมเต็มช่องว่างนี้ คลาสต่อไปนี้มีวิธีการเพิ่มเติม:

การตั้งชื่อสมาชิก API

เพื่อให้ใกล้ชิดกับโลก Python มากขึ้น สมาชิก API ของ Aspose.Words สำหรับ Python via .NET จะใช้รูปแบบงูหลาม อย่างไรก็ตาม ในกรณีส่วนใหญ่ สมาชิก API ของ Aspose.Words สำหรับ .NET API จะมีแบบอะนาล็อกหนึ่งต่อหนึ่ง คุณสามารถค้นหาแอนะล็อกเหล่านี้ได้ใน ไฟล์ xml