Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.Words for Python via .NET 是 Aspose.Words for .NET 的包装,这就是为什么这两个产品具有几乎相同的功能集。尽管如此,工作中还是存在一些细微差别以及功能和 API 之间的差异,本页对此进行了描述。
由于包装过程的原因,有些功能在 Python 版本中不可用。以下是 Python 版本中当前不可用的最显着功能的列表。
尽管类型转换对于 Python 开发人员来说并不自然,但如果不将文档节点或字段转换为具体类型,则无法完成某些任务。 Aspose.Words for 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() 方法。在 Aspose.Words Node 类的 Python 版本中引入了以下 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")以下代码示例演示如何读取 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 for Python 只允许使用 int 类型的索引器属性,但在 .NET 中也可以使用其他类型,例如字符串。为了填补这个空白,以下类有一个附加方法:
为了更接近 Python 世界,Aspose.Words for Python via .NET 的 API 成员使用 pythonic 蛇风格,但在大多数情况下,它们在 Aspose.Words for .NET API 中具有一对一的模拟。您可以在 xml文件 中找到这些类似物。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.