Crea PDF documenti o Word in Python

Crea un nuovo documento in quasi tutti i formati in modo programmatico utilizzando la nostra libreria ad alta fedeltà Python

Utilizzando la nostra API di programmazione, Python via .NET gli sviluppatori possono creare facilmente un documento in PDF, DOC, DOCX, HTML, EPUB e molti altri formati con poche righe di codice.

Visualizza lo snippet di codice

Crea un documento usando Python

Con la potente API fornita, gli sviluppatori Python possono creare documenti in quasi tutti i formati. Per fare ciò, è necessario seguire alcuni passaggi utilizzando la nostra libreria Python via .NET:

  1. Aggiungi il nome del file
  2. Inizia a creare un documento usando Python
  3. Salva il documento creato nel formato selezionato

Vale la pena notare che un documento vuoto dovrebbe tecnicamente contenere un paragrafo, quindi quando crei un documento a livello di codice, otterrai esattamente quella struttura di base del documento.

Tieni presente che puoi aggiungere istantaneamente contenuto a un documento appena creato. Pertanto, otterrai non solo un documento vuoto, ma un documento contenente il contenuto necessario. Per ulteriori informazioni su come modificare un documento, vedere la pagina Modifica.

Crea un documento in Python a livello di codice

La libreria Python via .NET fornita ti consente di creare a livello di codice un documento in qualsiasi formato supportato: PDF, DOCX, DOC, RTF, ODT, EPUB, HTML e altri.

Prova la nostra potente funzionalità e scopri come creare un documento in alcuni formati utilizzando il seguente esempio:

Crea un nuovo documento usando Python
Seleziona il formato di destinazione dall'elenco
Esegui codice
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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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")
# Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo.
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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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") # Se NextSibling è nullo, molto probabilmente questa è l'ultima Run del paragrafo. 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);
Esegui codice

Come creare un documento in Python

  1. Installa Aspose.Words for Python via .NET
  2. Aggiungi un riferimento alla libreria (importa la libreria) al tuo progetto Python
  3. Crea un nuovo documento
  4. Chiama il metodo save(), passando il nome del file
  5. Ottieni il risultato come file separato

Python libreria per creare documenti

Ospitiamo i nostri pacchetti Python nei repository PyPi. Segui le istruzioni passo passo su come installare "Aspose.Words for Python via .NET" nel tuo ambiente di sviluppo.

Requisiti di sistema

Questo pacchetto è compatibile con Python ≥3.5 e <3.12. Se sviluppi software per Linux, dai un'occhiata ai requisiti aggiuntivi per gcc e libpython nella documentazione del prodotto.

I formati di file più diffusi

5%

Iscriviti agli aggiornamenti del prodotto Aspose

Ricevi newsletter mensili e offerte direttamente nella tua casella di posta.

© Aspose Pty Ltd 2001-2024. Tutti i diritti riservati.