使用我們的 Python 庫,開發人員可以輕鬆地從頭開始製作 DOCX。為此,Python via .NET 開發人員只需執行幾個步驟:
值得注意的是,從技術上講,空白文檔應該包含一個段落,因此當您以編程方式創建 DOCX 文檔時,您將獲得完全一致的基本文檔結構。
請注意,您可以立即將內容添加到新創建的 DOCX 文件中。因此,您將獲得的不僅僅是一個空文檔,而是一個包含必要內容的文檔。有關如何編輯文檔的更多信息,請參閱編輯頁面。
此 Python via .NET 庫允許您以編程方式創建 DOCX 文檔。試試我們強大的功能,看看如何使用以下示例以某些格式創建 DOCX:
pip install aspose-words
複製
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
font = builder.font
font.name = "Courier New"
font.color = drawing.Color.blue
font.size = 36
font.highlight_color = drawing.Color.yellow
builder.write("Morbi enim nunc faucibus a.")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
save_options = aw.saving.PdfSaveOptions()
save_options.outline_options.bookmarks_outline_levels.add("Aspose bookmark", 1);
doc.Save("Output.docx", save_options);
import aspose.words as aw
doc = aw.Document()
run = aw.Run(doc, "Proin eros metus, sagittis sed.")
para = doc.first_section.body.first_paragraph
para.append_child(run)
comment = aw.Comment(doc)
comment.author = "John Doe"
comment.initial = "JD"
comment.date_time = datetime.now()
comment.set_text("Quisque fringilla leo.")
commentRangeStart = aw.CommentRangeStart(doc, comment.id)
commentRangeEnd = aw.CommentRangeEnd(doc, comment.id)
run.parent_node.insert_before(commentRangeStart, run)
run.parent_node.insert_after(commentRangeEnd, run)
commentRangeEnd.parent_node.insert_after(comment, commentRangeEnd)
comment.add_reply("Jane Doe", "JD", datetime.now(), "Pellentesque vel sapien justo.")
doc.save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
save_options = aw.saving.PdfSaveOptions()
save_options.outline_options.bookmarks_outline_levels.add("Aspose bookmark", 1);
doc.Save("Output.docx", save_options);
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
save_options = aw.saving.PdfSaveOptions()
save_options.outline_options.bookmarks_outline_levels.add("Aspose bookmark", 1);
doc.Save("Output.docx", save_options);
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_chart(aw.drawing.charts.ChartType.PIE, 432, 252)
chart = shape.chart
chart.title.text = "Demo Chart"
chart.series.clear()
chart.series.add("Series 1",
["Category1", "Category2", "Category3"],
[2.7, 3.2, 0.8])
doc.save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
doc.Save("Output.docx")
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
firstRun = aw.Run(doc, "Proin eros metus, sagittis sed. ")
secondRun = aw.Run(doc, "Morbi enim nunc faucibus a.")
doc.first_section.body.first_paragraph.append_child(firstRun)
doc.first_section.body.first_paragraph.append_child(secondRun)
builder.move_to(secondRun)
builder.start_bookmark("Aspose bookmark")
# 如果 NextSibling 為 null,則很可能這是該段落中的最後一次 Run 。
if (secondRun.next_sibling != None):
builder.move_to(secondRun.next_sibling)
else:
builder.move_to(secondRun.parent_paragraph)
builder.end_bookmark("Aspose bookmark")
save_options = aw.saving.PdfSaveOptions()
save_options.outline_options.bookmarks_outline_levels.add("Aspose bookmark", 1);
doc.Save("Output.docx", save_options);
我們在PyPi存儲庫中託管我們的 Python 請按照有關如何 "Aspose.Words for Python via .NET" 安裝到您的開發人員環境的分步說明進行操作。
這個包與 Python ≥3.5 和 <3.12 兼容。如果您為 Linux 開發軟件,請查看產品文檔中對 gcc 和 libpython 的附加要求。