如何绘制SmartArt图形,实现效果如下:
Snipaste_2025-11-03_17-44-36.png (2.0 KB)
Aspose.Words for Python (via .NET) – inserting and customizing SmartArt
Below is a complete, ready‑to‑run example that creates a new DOCX, inserts a Basic Cycle SmartArt diagram, fills each shape with custom text, tweaks a few visual properties, and saves the result in several formats.
# -------------------------------------------------
# Aspose.Words for Python via .NET – SmartArt demo
# -------------------------------------------------
import aspose.words as aw
# (Optional) set your license – otherwise a trial watermark is added
# aw.License().set_license("Aspose.Words.lic")
# 1. Create an empty document and obtain a DocumentBuilder
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# 2. Insert SmartArt – here we use the BASIC_CYCLE layout
#    Other layouts are available in the aw.SmartArtLayout enum
smart_art = builder.insert_smart_art(aw.SmartArtLayout.BASIC_CYCLE)
# 3. Populate the nodes with our own text
node_texts = ["Step 1", "Step 2", "Step 3", "Step 4"]
for i, node in enumerate(smart_art.all_nodes):
    # Remove the default placeholder text
    node.text_frame.paragraphs.clear()
    # Add a new paragraph and a Run that contains the desired text
    para = node.text_frame.paragraphs.add()
    para.append_child(aw.Run(doc, node_texts[i]))
    # (Optional) format the text
    run = para.runs[0]
    run.font.color = aw.Color.blue
    run.font.size = 12
# 4. (Optional) adjust the SmartArt shape itself
smart_art.shape.fill.fore_color = aw.Color.light_yellow   # background colour
smart_art.shape.line.format.width = 1.5                  # border thickness
smart_art.shape.line.format.color = aw.Color.dark_gray
# 5. Save the document – you can choose any of the supported formats
doc.save("SmartArtDemo.docx")   # Word document
doc.save("SmartArtDemo.pdf")    # PDF
doc.save("SmartArtDemo.png")    # PNG (one image per page)
print("SmartArt inserted and files saved successfully.")
How the code works
| Step | Key API | What it does | 
|---|---|---|
| Create document | aw.Document(), aw.DocumentBuilder(doc) | 
Starts with a blank Word file. | 
| Insert SmartArt | builder.insert_smart_art(layout) | 
layout is a value from aw.SmartArtLayout (e.g., BASIC_CYCLE, VERTICAL_BULLET_LIST). | 
| Access nodes | smart_art.all_nodes | 
Returns a SmartArtNodeCollection; each node represents one shape in the diagram. | 
| Write text | node.text_frame.paragraphs.clear(), node.text_frame.paragraphs.add(), aw.Run(doc, text) | 
Clears the placeholder, adds a paragraph, and inserts a Run with the desired string. | 
| Format text | run.font.color, run.font.size | 
Full Word text‑formatting capabilities. | 
| Shape appearance | smart_art.shape.fill.fore_color, smart_art.shape.line.format.* | 
Adjust fill colour, line width, line colour, etc. | 
| Save | doc.save(path) | 
The file extension determines the output format (DOCX, PDF, PNG, JPEG, TIFF, …). | 
Frequently asked questions
| Issue | Remedy | 
|---|---|
SmartArtLayout not found | 
Make sure you are using Aspose.Words ≥ 25.0 and imported the library as import aspose.words as aw. | 
| Need a different number of nodes | Some layouts have a fixed node count. Choose a layout that supports a variable number of nodes (e.g., the PROCESS family) or add/remove nodes programmatically: smart_art.all_nodes.add() / smart_art.all_nodes.remove_at(index). | 
| Saved PNG is blank | Call doc.update_page_layout() before doc.save("file.png") to force pagination, and ensure the document contains only the SmartArt on a single page. | 
| Appearance differs from Word UI | Use the shape object to fine‑tune fill, line, shadow, and 3‑D effects, or create a DOCX template in Word, insert the SmartArt manually, and then modify only the node text via code. | 
Quick checklist
- Install the package – 
pip install aspose-words. - Add a license (optional but removes the evaluation watermark).
 - Use the 
insert_smart_artmethod with a layout fromaw.SmartArtLayout. - Iterate 
smart_art.all_nodesto set text and formatting. - Save in the desired format.
 
With these steps you can generate any of the built‑in SmartArt diagrams programmatically and export them to Word, PDF, images, or other supported formats. Happy coding!