Tạo PDF tài liệu hoặc Word trong Python

Tạo tài liệu mới ở hầu hết mọi định dạng theo chương trình bằng cách sử dụng thư viện Python có độ trung thực cao của chúng tôi

Sử dụng API lập trình của chúng tôi, các nhà phát triển Python via .NET có thể dễ dàng tạo tài liệu ở PDF định dạng, DOC, DOCX, HTML, EPUB và nhiều định dạng khác chỉ với một vài dòng mã.

Xem đoạn mã

Tạo tài liệu bằng Python

Với API mạnh mẽ đã cho, các nhà phát triển Python có thể tạo tài liệu ở hầu hết mọi định dạng. Để thực hiện việc này, bạn cần làm theo một số bước bằng cách sử dụng thư viện Python via .NET của chúng tôi:

  1. Thêm tên tệp
  2. Bắt đầu tạo tài liệu bằng Python
  3. Lưu tài liệu đã tạo ở định dạng đã chọn

Đ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 một 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ể ngay lập tức thêm nội dung vào một tài liệu 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 tài liệu bằng Python theo chương trình

Thư viện Python via .NET nhất định cho phép bạn lập trình tạo tài liệu ở bất kỳ định dạng nào được hỗ trợ - PDF, DOCX, DOC, RTF, ODT, EPUB, HTML và các định dạng khác.

Hãy thử chức năng mạnh mẽ của chúng tôi và xem cách tạo tài liệu ở một số định dạng bằng cách sử dụng ví dụ sau:

Tạo tài liệu mới bằ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 tài liệu 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 một 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 tài liệu

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 phổ biến nhất

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.