Tạo DOCX bằng cách sử dụng Python via .NET thư viện

Thư viện Python mạnh mẽ của chúng tôi cho phép các nhà phát triển tạo lập trình DOCX chỉ trong vài bước

Python via .NET các nhà phát triển có thể dễ dàng thực hiện DOCX chỉ bằng cách sử dụng API sản phẩm mạnh mẽ của chúng tôi. Có nghĩa là giải pháp của chúng tôi sẽ cung cấp cho các lập trình viên mọi thứ họ cần để tạo DOCX trong Python.

Xem đoạn mã

Tạo DOCX trong Python theo chương trình

Với thư viện Python của chúng tôi, các nhà phát triển có thể dễ dàng tạo DOCX từ đầu. Để làm điều này, các nhà phát triển Python via .NET chỉ cần thực hiện một vài bước:

  1. Thêm tên tệp
  2. Bắt đầu tạo DOCX tài liệu bằng Python
  3. Lưu DOCX tệp đầu ra

Điều đáng chú ý là về mặt kỹ thuật, một tài liệu trống được cho là chứa một đoạn văn, vì vậy khi bạn tạo DOCX tài liệu theo chương trình, bạn sẽ nhận được chính xác cấu trúc tài liệu cơ bản đó.

Lưu ý rằng bạn có thể thêm ngay nội dung vào DOCX tệp mới được tạo. Vì vậy, bạn sẽ nhận được không chỉ là một tài liệu trống, mà là một tài liệu có chứa nội dung cần thiết. Để biết thêm thông tin về cách chỉnh sửa tài liệu, hãy xem trang Chỉnh sửa.

Tạo DOCX trong Python

Thư viện Python via .NET này cho phép bạn tạo DOCX tài liệu theo chương trình. Hãy thử chức năng mạnh mẽ của chúng tôi và xem cách tạo DOCX ở một số định dạng bằng cách sử dụng ví dụ sau:

Tạo một DOCX mới bằng cách sử dụng Python
Chọn định dạng mục tiêu từ danh sách
Mã vận hành
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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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")
# Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn.
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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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") # Nếu NextSibling là null thì rất có thể đây là Run cuối cùng trong Đoạn. 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);
Mã vận hành

Cách tạo DOCX trong Python

  1. Cài đặt Aspose.Words for Python via .NET
  2. Thêm tham chiếu thư viện (nhập thư viện) vào dự án Python của bạn
  3. Tạo DOCX tài liệu mới
  4. Gọi phương thức save(), chuyển tên tệp
  5. Nhận kết quả dưới dạng một tệp riêng biệt

Python thư viện để tạo DOCX

Chúng tôi lưu trữ các gói Python trong kho lưu trữ PyPi. Vui lòng làm theo hướng dẫn từng bước về cách cài đặt "Aspose.Words for Python via .NET" vào môi trường nhà phát triển của bạn.

yêu cầu hệ thống

Gói này tương thích với Python ≥3.5 và <3.12. Nếu bạn phát triển phần mềm cho Linux, vui lòng xem các yêu cầu bổ sung đối với gcc và libpython trong Tài liệu sản phẩm.

Các định dạng tệp được hỗ trợ khác

Bạn có thể tạo tài liệu ở các định dạng tệp khác:

5%

Đăng ký cập nhật sản phẩm của Aspose

Nhận bản tin hàng tháng và ưu đãi gửi trực tiếp đến hộp thư của bạn.

© Aspose Pty Ltd 2001-2024. Đã đăng ký Bản quyền.