Knowledgebase

Tabelle in Python | Aspose

In questo articolo lavoreremo con le tabelle in Python, mostrandoti il modo più semplice per creare una tabella e manipolare i dati al suo interno. Utilizzando la nostra API, puoi manipolare le tabelle ed esportarle in diversi formati, come Word, Excel, PDF, ecc.
Presenteremo Aspose.Words for Python via .NET, concentrandoci sulla creazione e l’inserimento di tabelle tramite questa API.

Tabelle in Python

Aspose.Words for Python via .NET è una libreria di classi progettata per leggere e manipolare documenti di numerosi tipi come Microsoft Word (DOCX, DOC, ODT), Web (Markdown, HTML), PDF e altri. Con questa API Python, puoi creare, modificare, renderizzare e convertire file Word in più formati, generare report e visualizzare i tuoi dati senza la necessità di software esterni.
A tua disposizione ci sono più di 100 classi Python per la gestione dell’elaborazione dei documenti e delle operazioni di formattazione dei dati.

Lavorare con le tabelle in Python

Le tabelle consentono di organizzare grandi quantità di informazioni e di visualizzarle in una struttura a griglia con righe e colonne. Sono molto utili quando si mostrano dati tabulari, poiché permettono un controllo molto migliore sul design del contenuto.
Le tabelle sono insiemi strutturati di dati composti da righe e colonne. Sono pienamente supportate nella nostra libreria e puoi facilmente modificare, cambiare, aggiungere e rimuovere tabelle.
Di seguito ti mostriamo come creare una nuova tabella usando Python e, nella nostra documentazione, puoi vedere come applicare la formattazione , lavorare con TableStyle , lavorare con colonne e righe , e unire e dividere tabelle .

Creare tabelle in Python

La nostra libreria offre diversi metodi per creare nuove tabelle all’interno di un documento; in questo articolo vedremo come utilizzare alcuni di questi metodi.
La tabella appena creata ha valori simili a quelli predefiniti di Microsoft Word.

Proprietà della tabella Python

Puoi inserire una tabella usando la classe DocumentBuilder e i seguenti metodi per costruire la tabella:
DocumentBuilder.start_table
DocumentBuilder.insert_cell
DocumentBuilder.end_row
DocumentBuilder.end_table
DocumentBuilder.writeln

Metodi di costruzione della tabella Python

Crea una tabella in Python usando questi 7 passaggi:

  1. Avvia la tabella con DocumentBuilder.start_table.
  2. Usa DocumentBuilder.insert_cell per inserire una cella nella tabella e utilizza DocumentBuilder.cell_format per specificare la formattazione della cella.
  3. Usa il DocumentBuilder per inserire il contenuto della cella.
  4. Ripeti i passaggi 2 e 3 fino a completare la riga.
  5. Puoi chiamare il metodo DocumentBuilder.end_row per terminare la riga e usare DocumentBuilder.row_format per formattare la riga.
  6. Dopo ciò, ripeti i passaggi 2‑5 fino a completare la tabella.
  7. Usa il metodo DocumentBuilder.end_table per terminare la tabella.

Maggiori dettagli sulla classe DocumentBuilder e sui metodi per la creazione di tabelle sono descritti nella nostra documentazione .

Nell’esempio seguente puoi vedere come creare una tabella semplice con formattazione predefinita usando DocumentBuilder.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
    
# Start building the table.
builder.start_table()
builder.insert_cell()
builder.write("Row 1, Cell 1 Content.")
    
# Build the second cell.
builder.insert_cell()
builder.write("Row 1, Cell 2 Content.")
    
# Call the following method to end the row and start a new row.
builder.end_row()

# Build the first cell of the second row.
builder.insert_cell()
builder.write("Row 2, Cell 1 Content")

# Build the second cell.
builder.insert_cell()
builder.write("Row 2, Cell 2 Content.")
builder.end_row()

# Signal that we have finished building the table.
builder.end_table()

doc.save(docs_base.artifacts_dir + "WorkingWithTables.create_simple_table.docx")

Crea una tabella formattata usando DocumentBuilder con il codice di esempio mostrato di seguito.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)

table = builder.start_table()
builder.insert_cell()

# Table wide formatting must be applied after at least one row is present in the table.
table.left_indent = 20.0

# Set height and define the height rule for the header row.
builder.row_format.height = 40.0
builder.row_format.height_rule = aw.HeightRule.AT_LEAST

builder.cell_format.shading.background_pattern_color = drawing.Color.from_argb(198, 217, 241)
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
builder.font.size = 16
builder.font.name = "Arial"
builder.font.bold = True

builder.cell_format.width = 100.0
builder.write("Header Row,\n Cell 1")

# We don't need to specify this cell's width because it's inherited from the previous cell.
builder.insert_cell()
builder.write("Header Row,\n Cell 2")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Header Row,\n Cell 3")
builder.end_row()

builder.cell_format.shading.background_pattern_color = drawing.Color.white
builder.cell_format.width = 100.0
builder.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER

# Reset height and define a different height rule for table body.
builder.row_format.height = 30.0
builder.row_format.height_rule = aw.HeightRule.AUTO
builder.insert_cell()
    
# Reset font formatting.
builder.font.size = 12
builder.font.bold = False

builder.write("Row 1, Cell 1 Content")
builder.insert_cell()
builder.write("Row 1, Cell 2 Content")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Row 1, Cell 3 Content")
builder.end_row()

builder.insert_cell()
builder.cell_format.width = 100.0
builder.write("Row 2, Cell 1 Content")

builder.insert_cell()
builder.write("Row 2, Cell 2 Content")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Row 2, Cell 3 Content.")
builder.end_row()
builder.end_table()

doc.save(docs_base.artifacts_dir + "WorkingWithTables.formatted_table.docx")

Inserire una tabella esistente

Se hai già una tabella nel documento e vuoi aggiungere una copia di essa per apportare delle modifiche, il modo più semplice per duplicare una tabella è utilizzare il metodo Table.clone per mantenere tutta la formattazione.
Nell’esempio mostrato di seguito, puoi vedere come inserire una tabella; se ti serve il file modello di questo esempio, puoi scaricarlo da qui .

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Tables.docx")

table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()

# Clone the table and insert it into the document after the original.
tableClone = table.clone(True).as_table()
table.parent_node.insert_after(tableClone, table)

# Insert an empty paragraph between the two tables,
# or else they will be combined into one upon saving this has to do with document validation.
table.parent_node.insert_after(aw.Paragraph(doc), table)
    
doc.save(docs_base.artifacts_dir + "WorkingWithTables.clone_complete_table.docx")

Se hai domande sulle tabelle, sui prodotti Aspose.Words for Python via .NET e hai bisogno di assistenza da parte dei nostri sviluppatori del Supporto a Pagamento per implementare la nostra API nel tuo progetto, sentiti libero di contattaci .