Knowledgebase (2329)
Children categories
Merge tables in Word can be useful when you want to combine data from multiple tables into a single, larger table to create a more comprehensive view of the information. On the contrary, split tables can help you divide a large table into smaller, more manageable sections so you can focus on specific data sets. This article will demonstrate how to merge or split tables in Word in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Merge Tables in Word in Python
With Spire.Doc for Python, you can combine two or more tables into one by copying all rows from other tables to the target table and then deleting the other tables. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Get two tables in the section using Section.Tables[] property.
- Iterate through all rows in the second table and copy them using Table.Rows[].Clone() method.
- Add the rows of the second table to the first table using Table.Rows.Add() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
inputFile = "Cost.docx"
outputFile = "CombineTables.docx"
# Create a Document instance
doc = Document()
# Load a Word document
doc.LoadFromFile(inputFile)
# Get the first section
section = doc.Sections[0]
# Get the first and second table in the section
table1 = section.Tables[0] if isinstance(section.Tables[0], Table) else None
table2 = section.Tables[1] if isinstance(section.Tables[1], Table) else None
# Add rows of the second table to the first table
for i in range(table2.Rows.Count):
table1.Rows.Add(table2.Rows[i].Clone())
# Remove the second table
section.Tables.Remove(table2)
# Save the result document
section.Document.SaveToFile(outputFile, FileFormat.Docx2013)
doc.Close()

Spilt a Table in Word in Python
To split a table into two or more tables, you need to create a new table, then copy the specified rows from the original table to the new table, and then delete those rows from the original table. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Get a specified table in the section using Section.Tables[] property.
- Specify the row index where the table will be split.
- Create a new instance of the Table class.
- Iterate through the specified rows in the original table and copy them using Table.Rows[].Clone() method.
- Add the specified rows to the new table using Table.Rows.Add() method.
- Iterate through the copied rows and remove each row from the original table using Table.Rows.RemoveAt() method.
- Add the new table to the section using Section.Tables.Add() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
inputFile = "CombineTables.docx"
outputFile = "SplitTable.docx"
# Create a Document instance
doc = Document()
# Load a Word document
doc.LoadFromFile(inputFile)
# Get the first section
section = doc.Sections[0]
# Get the first table in the section
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# Specify to split the table from the fifth row
splitIndex = 4
# Create a new table
newTable = Table(section.Document, True)
# Adds rows (from the 5th to the last row) to the new table
for i in range(splitIndex, table.Rows.Count):
newTable.Rows.Add(table.Rows[i].Clone())
# Delete rows from the original table
for i in range(table.Rows.Count - 1, splitIndex - 1, -1):
table.Rows.RemoveAt(i)
# Add the new table to the section
section.Tables.Add(newTable)
# Save the result document
section.Document.SaveToFile(outputFile, FileFormat.Docx2013)
doc.Close()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
The inclusion of line numbers in Word documents serves as a critical tool for enhancing readability, facilitating reference, and streamlining collaborative editing processes. Whether you're a lawyer marking up contracts, a researcher annotating scientific papers, or a student revising a thesis, line numbers provide a precise way to cite specific lines, making discussions and revisions more efficient.
The powerful Python programming language enables users to batch add or remove line numbers in Word documents, providing a robust means to automate document preparation workflows. This article will demonstrate how to utilize Spire.Doc for Python to add or remove line numbers in Word documents with Python code.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to: How to Install Spire.Doc for Python on Windows
Add Line Numbers to Word Documents with Python
Spire.Doc for Python provides properties under PageSetup class for line number formatting. The properties and their functions are as follows:
- LineNumberingStep: Used to set the interval of the line number display.
- LineNumberingStartValue: Used to set the start number of the line number.
- LineNumberingDistanceFromText: Used to set the distance between the line number and the text.
- LineNumberingRestartMode: Used to set when the line number restarts, like every page, every section, or continuously without restarting.
It is important to note that line numbers will only be displayed when the PageSetup.LineNumberingStep property is set to a value greater than 0.
The detailed steps for adding line numbers to Word documents are as follows:
- Create an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Iterate through the sections in the document:
- Get the current section using Document.Sections.get_Item() method.
- Get the page setup of the section through Section.PageSetup property.
- Set the display interval of the line numbers through PageSetup.LineNumberingStep property.
- Set the start number of the line numbers through PageSetup.LineNumberingStartValue property.
- Set the distance between line numbers and text through PageSetup.LineNumberingDistanceFromText property.
- Set the restarting mode of the line numbers through PageSetup.LineNumberingRestartMode property.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Iterate through the sections
for i in range(0, doc.Sections.Count):
# Get the current section
section = doc.Sections.get_Item(i)
# Get the page setup of the section
pageSetup = section.PageSetup
# Set the interval of the line numbering
pageSetup.LineNumberingStep = 2
# Set the start number of the line numbering
pageSetup.LineNumberingStartValue = 1
# Set the distance between the line number and text
pageSetup.LineNumberingDistanceFromText = 20
# Set the restarting mode of the line number
pageSetup.LineNumberingRestartMode = LineNumberingRestartMode.Continuous
# Save the document
doc.SaveToFile("output/AddLineNumberWord.docx", FileFormat.Docx)
doc.Close()

Remove Line Numbers from Word Documents with Python
Since the value of the PageSetup.LineNumberingStep property directly determines the display of line numbers, developers can simply set the value to 0 to remove the line numbers from Word documents.
The detailed steps for removing line numbers from a Word document are as follows:
- Create an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Iterate through the sections in the document:
- Get the current section using Document.Sections.get_Item() method.
- Set the display interval of the line numbers to 0 through Section.PageSetup.LineNumberingStep property to remove the line numbers.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("output/AddLineNumberWord.docx")
# Iterate through the sections
for i in range(0, doc.Sections.Count):
# Get the current section
section = doc.Sections.get_Item(i)
# Set the interval of the line numbering to 0 to remove the line numbering
section.PageSetup.LineNumberingStep = 0
# Save the document
doc.SaveToFile("output/RemoveLineNumberWord.docx", FileFormat.Docx)
doc.Close()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
PDF (Portable Document Format) files are widely used for sharing and distributing documents due to their consistent formatting and broad compatibility. However, when it comes to presentations, PowerPoint remains the preferred format for many users. PowerPoint offers a wide range of features and tools that enable the creation of dynamic, interactive, and visually appealing slideshows. Unlike static PDF documents, PowerPoint presentations allow for the incorporation of animations, transitions, multimedia elements, and other interactive components, making them more engaging and effective for delivering information to the audience.
By converting PDF to PowerPoint, you can transform a static document into a captivating and impactful presentation that resonates with your audience and helps to achieve your communication goals. In this article, we will explain how to convert PDF files to PowerPoint format in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Convert PDF to PowerPoint in Python
Spire.PDF for Python provides the PdfDocument.SaveToFile(filename:str, FileFormat.PPTX) method to convert a PDF document into a PowerPoint presentation. With this method, each page of the original PDF document will be converted into a single slide in the output PPTX presentation.
The detailed steps to convert a PDF document to PowerPoint format are as follows:
- Create an object of the PdfDocument class.
- Load a sample PDF document using the PdfDocument.LoadFromFile() method.
- Save the PDF document as a PowerPoint PPTX file using the PdfDocument.SaveToFile(filename:str, FileFormat.PPTX) method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a sample PDF document
pdf.LoadFromFile("Sample.pdf")
# Save the PDF document as a PowerPoint PPTX file
pdf.SaveToFile("PdfToPowerPoint.pptx", FileFormat.PPTX)
pdf.Close()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.