|
import aspose.words as aw |
|
|
|
# This code example demonstrates how to create a new Word document using Python. |
|
# Create document object |
|
doc = aw.Document() |
|
|
|
# Create a document builder object |
|
builder = aw.DocumentBuilder(doc) |
|
|
|
# Specify font formatting Font |
|
font = builder.font |
|
font.size = 32 |
|
font.bold = True |
|
font.name = "Arial" |
|
font.underline = aw.Underline.SINGLE |
|
|
|
# Insert text |
|
builder.writeln("Welcome") |
|
builder.writeln() |
|
|
|
# Set paragraph formatting |
|
font.size = 14 |
|
font.bold = False |
|
font.name = "Arial" |
|
font.underline = aw.Underline.NONE |
|
|
|
paragraphFormat = builder.paragraph_format |
|
paragraphFormat.first_line_indent = 8 |
|
paragraphFormat.alignment = aw.ParagraphAlignment.JUSTIFY |
|
paragraphFormat.keep_together = True |
|
|
|
# Insert paragraph |
|
builder.writeln('''Aspose.Words for Python is a class library that enables your applications to perform a great range of document processing tasks. |
|
It supports most of the popular document formats such as DOC, DOCX, RTF, HTML, Markdown, PDF, XPS, EPUB, and others. |
|
With the API, you can generate, modify, convert, render, and print documents without third-party applications or Office Automation. |
|
''') |
|
builder.writeln() |
|
|
|
# Insert a Table |
|
font.bold = True |
|
builder.writeln("This is a sample table") |
|
font.bold = False |
|
|
|
# Start table |
|
table = builder.start_table() |
|
|
|
# Insert cell |
|
builder.insert_cell() |
|
table.auto_fit(aw.tables.AutoFitBehavior.AUTO_FIT_TO_CONTENTS) |
|
|
|
# Set formatting and add text |
|
builder.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER |
|
|
|
builder.write("Row 1 cell 1") |
|
builder.insert_cell() |
|
builder.write("Row 1 cell 2") |
|
builder.end_row() |
|
|
|
builder.insert_cell() |
|
builder.write("Row 2 cell 1") |
|
builder.insert_cell() |
|
builder.write("Row 2 cell 2") |
|
builder.end_row() |
|
|
|
# End table |
|
builder.end_table() |
|
builder.writeln() |
|
|
|
# Insert image |
|
builder.insert_image("C:\\Files\\aspose-icon.png") |
|
|
|
# Save document |
|
doc.save("C:\\Files\\sample_output.docx") |