V tomto článku budeme pracovat s tabulkami v Pythonu, ukážeme vám nejjednodušší způsob, jak vytvořit tabulku a manipulovat s daty v ní. Pomocí našeho API můžete manipulovat s tabulkami a exportovat je do různých formátů, jako jsou Word, Excel, PDF atd.
Představíme Aspose.Words pro Python via .NET a zaměříme se na vytváření a vkládání tabulek pomocí tohoto API.

Aspose.Words pro Python via .NET
je knihovna tříd navržená pro čtení a manipulaci s dokumenty různých typů, jako je Microsoft Word (DOCX, DOC, ODT), web (Markdown, HTML), PDF a další. S tímto Python API můžete vytvářet, upravovat, renderovat a převádět soubory Word do mnoha formátů, generovat zprávy a vizualizovat svá data bez potřeby externího softwaru.
K dispozici máte více než 100 tříd Pythonu pro zpracování dokumentů a operace formátování dat.
Práce s tabulkami v Pythonu
Tabulky umožňují organizovat velké množství informací a zobrazovat je v mřížkové struktuře s řádky a sloupci. Jsou velmi užitečné při zobrazování tabulkových dat, protože poskytují mnohem lepší kontrolu nad návrhem obsahu.
Tabulky jsou strukturované sady dat složené z řádků a sloupců. Jsou plně podporovány v naší knihovně a můžete je snadno upravovat, měnit, přidávat i odstraňovat.
Níže vám ukážeme, jak vytvořit novou tabulku pomocí Pythonu, a v naší dokumentaci můžete vidět, jak aplikovat formátování
, pracovat s TableStyle
, pracovat se sloupci a řádky
, a sloučit a rozdělit tabulky
.
Vytváření tabulek v Pythonu
Naše knihovna nabízí různé metody pro vytváření nových tabulek v dokumentu a v tomto článku uvidíme, jak některé z těchto metod použít.
Nově vytvořená tabulka má hodnoty podobné výchozím hodnotám Microsoft Wordu.

Tabulku můžete vložit pomocí třídy DocumentBuilder
, a následujících metod pro vytvoření tabulky:
• DocumentBuilder.start_table
• DocumentBuilder.insert_cell
• DocumentBuilder.end_row
• DocumentBuilder.end_table
• DocumentBuilder.writeln

Vytvořte tabulku v Pythonu pomocí těchto 7 kroků:
- Spusťte tabulku pomocí DocumentBuilder.start_table.
- Použijte DocumentBuilder.insert_cell k vložení buňky do tabulky a použijte DocumentBuilder.cell_format k určení formátování buňky.
- Použijte DocumentBuilder k vložení obsahu buňky.
- Jednoduše opakujte kroky 2 a 3, dokud nedokončíte řádek.
- Můžete zavolat metodu DocumentBuilder.end_row k ukončení řádku a použít DocumentBuilder.row_format pro formátování řádku.
- Poté opakujte kroky 2 až 5, dokud nedokončíte tabulku.
- Použijte metodu DocumentBuilder.end_table k dokončení tabulky.
Více podrobností o třídě DocumentBuilder a metodách pro vytváření tabulek najdete v naší dokumentaci
.
V níže uvedeném příkladu můžete vidět, jak vytvořit jednoduchou tabulku s výchozím formátováním pomocí 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")
Vytvořte formátovanou tabulku pomocí DocumentBuilder
s ukázkovým kódem níže.
# 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")
Vkládání existující tabulky
Pokud již máte v dokumentu tabulku a chcete přidat její kopii, abyste ji mohli upravit, nejjednodušší způsob, jak tabulku duplikovat, je použití metody Table.clone
k zachování veškerého formátování.
V níže uvedeném příkladu můžete vidět, jak vložit tabulku, a pokud potřebujete šablonový soubor tohoto příkladu, můžete jej stáhnout zde
.
# 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")
Pokud máte otázky ohledně tabulek, produktů Aspose.Words pro Python via .NET a potřebujete pomoc od našich vývojářů placené podpory
při implementaci našeho API ve vašem projektu, neváhejte nás kontaktovat
.