Spire.Office Knowledgebase Page 41 | E-iceblue

In Word documents, the ability to add, modify, and remove table borders flexibly can significantly enhance readability and professionalism. Firstly, customizing border styles highlights important information, helping readers quickly locate key data or paragraphs and enhancing visual impact. Secondly, by adjusting the thickness, color, and style of border lines, finer design control can be achieved, ensuring a uniform and aesthetically pleasing document style. Lastly, removing unnecessary borders helps reduce visual clutter, making page layouts cleaner and more comprehensible, improving the reading experience. This article will introduce how to add, modify, or remove Word table borders in Java projects using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.11.2</version>
    </dependency>
</dependencies>

Java Add Word Table Borders

To uniformly add borders to all cells in a table, you need to visit each cell individually and visually set its border properties. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document using Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Use a for loop to iterate through all cells in the table.
  • Set the cell border to a single line style by using TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single).
  • Define the border width to 1 point by using TableCell.getCellFormat().getBorders().setLineWidth(1f).
  • Set the border color to black by using TableCell.getCellFormat().getBorders().setColor(Color.black).
  • Save the changes to the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class AddBorder {
    public static void main(String[] args) throws Exception{
        // Create a new Document object
        Document doc = new Document();

        // Load the document from file
        doc.loadFromFile("TableExample1.docx");

        // Get the first section of the document
        Section section = doc.getSections().get(0);

        // Get the first table in the section
        Table table = section.getTables().get(0);

        // Declare TableRow and TableCell variables for use in the loop
        TableRow tableRow;
        TableCell tableCell;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            // Get the current row
            tableRow = table.getRows().get(i);

            // Iterate through all cells in the current row
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Get the current cell
                tableCell = tableRow.getCells().get(j);

                // Set the border style of the current cell to single line
                tableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);

                // Set the width of the border
                tableCell.getCellFormat().getBorders().setLineWidth(1f);

                // Set the color of the border
                tableCell.getCellFormat().getBorders().setColor(Color.black);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("AddBorders.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Modify Word Table Borders

Spire.Doc empowers users with extensive customization options for borders, allowing adjustments such as selecting border styles through TableCell.getCellFormat().getBorders().getBottom().setBorderType(), setting border thickness via TableCell.getCellFormat().getBorders().getBottom().setLineWidth(), and specifying border colors with TableCell.getCellFormat().getBorders().getBottom().setColor(). This enables fine-tuned design of table borders within documents according to personal or project needs. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Iterate over the cells in the table that require border style changes using a for loop.
  • Change the color of the bottom border to orange by invoking TableCell.getCellFormat().getBorders().getBottom ().setColor(Color.ORANGE).
  • Alter the style of the bottom border to a dashed line by calling TableCell.getCellFormat().getBorders().getBottom ().setBorderType(BorderStyle.Dot_Dash).
  • Modify the width of the bottom border to 2 points by executing TableCell.getCellFormat().getBorders().getBottom ().setLineWidth(2).
  • Save the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class ModifyBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

        // Get the first section of the document
        Section section = doc.getSections().get(0);

        // Get the first table in that section
        Table table = section.getTables().get(0);

        // Declare a TableRow to use within the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 1; i < table.getRows().getCount() - 1; i++) {
            tableRow = table.getRows().get(i);

            // Set the border color of the current cell     tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setColor(Color.ORANGE);
            // Set the border style of the current cell to dotted line
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);

            // Set the width of the border
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setLineWidth(2);
        }

        // Save the modified document as a new file
        doc.saveToFile("ModifyBorder.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Remove Word Table Borders

When editing Word documents, the flexibility of border design extends beyond the entire table level, allowing for meticulous personalized adjustments at the individual cell level. To comprehensively remove all traces of borders both inside and outside of tables, a phased approach is recommended: Firstly, address the macro-level by clearing the overall border style of the table; subsequently, enter the micro-adjustment phase where each cell within the table is iterated over to revoke its unique border settings. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Access the first table within the section using Section.getTables().get(0).
  • Iterate over all cells in the table using a for loop.
  • Remove the border of the table by invoking Table.getTableFormat().getBorders().setBorderType(BorderStyle.None).
  • Eliminate the borders of each cell individually by applying TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.None).
  • Save the modified document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;

public class RemoveBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

        // Get the first section of the document
        Section section = doc.getSections().get(0);

        // Get the first table in the section
        Table table = section.getTables().get(0);

        // Remove the borders set on the table
        table.getTableFormat().getBorders().setBorderType(BorderStyle.None);

        // Declare a TableRow to use in the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            tableRow = table.getRows().get(i);
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Remove all borders set on the cell          tableRow.getCells().get(j).getCellFormat().getBorders().setBorderType(BorderStyle.None);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("RemoveBorder.docx", FileFormat.Docx);

        // Close the document and release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

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.

Copying data in Excel is a fundamental feature that allows you to quickly and efficiently reproduce data. It can be especially valuable when building spreadsheets with similar structures, or needing to propagate the same information across multiple areas of your workbook. By mastering the art of copying in Excel, you can boost your productivity and reduce the risk of manual data entry errors. In this article, we will explain how to copy rows, columns and cells in Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Copy Rows in Excel in Python

You can use the Worksheet.CopyRow(sourceRow, destSheet, destRowIndex, copyOptions) method provided by Spire.XLS for Python to easily copy a row in the same or between different worksheets in Excel. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get the source worksheet and the destination worksheet using the Workbook.Worksheets[index] property.
  • Get the desired row that you want to copy using the Worksheet.Rows[index] property.
  • Copy the row and its format from the source worksheet to the destination worksheet using the Worksheet.CopyRow(sourceRow, destSheet, destRowIndex, copyOptions) method.
  • Copy the column widths of cells in the source row to the corresponding cells in the destination row.
  • Save the workbook to a file using the Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("ContactList.xlsx")

# Get the source worksheet
sheet1 = workbook.Worksheets[0] 
# Get the destination worksheet
sheet2 = workbook.Worksheets[1]

# Get the desired row that you want to copy
row = sheet1.Rows[0]

# Copy the row from the source worksheet to the first row of the destination worksheet
sheet1.CopyRow(row, sheet2, 1, CopyRangeOptions.All)

columns = sheet1.Columns.Length
# Copy the column widths of the cells in the source row to the corresponding cells in the destination row
for i in range(columns):
    column_width = row.Columns[i].ColumnWidth
    sheet2.Rows[0].Columns[i].ColumnWidth = column_width

# Save the workbook to a file
workbook.SaveToFile("CopyRow.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Copy Rows, Columns and Cells in Excel

Copy Columns in Excel in Python

To copy a column in an Excel worksheet, you can use the Worksheet.CopyColumn(sourceColumn, destSheet, destColIndex, copyOptions) method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get the source worksheet and the destination worksheet using the Workbook.Worksheets[index] property.
  • Get the desired column that you want to copy using the Worksheet.Columns[index] property.
  • Copy the column and its format from the source worksheet to the destination worksheet using the Worksheet.CopyColumn(sourceColumn, destSheet, destColIndex, copyOptions) method.
  • Copy the row heights of cells in the source column to the corresponding cells in the destination column.
  • Save the workbook to a file using the Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("ContactList.xlsx")

# Get the source worksheet
sheet1 = workbook.Worksheets[0] 
# Get the destination worksheet
sheet2 = workbook.Worksheets[1]

# Get the desired column that you want to copy
column = sheet1.Columns[0]

# Copy the column from the source worksheet to the first column of the destination worksheet
sheet1.CopyColumn(column, sheet2, 1, CopyRangeOptions.All)

rows = column.Rows.Length
# Copy the row heights of cells in the source column to the corresponding cells in the destination column
for i in range(rows):
    row_height = column.Rows[i].RowHeight
    sheet2.Columns[0].Rows[i].RowHeight = row_height

# Save the workbook to a file
workbook.SaveToFile("CopyColumn.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Copy Rows, Columns and Cells in Excel

Copy Cells in Excel in Python

In addition to copying entire rows and columns, you are also able to copy an individual cell or a range of cells using the CellRange.Copy(destRange, copyOptions) method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get the source worksheet and the destination worksheet using the Workbook.Worksheets[index] property.
  • Get the source cell range and the destination cell range using the Worksheet.Range[] property.
  • Copy the source cell range and its format from the source worksheet to the destination cell range in the destination worksheet using the CellRange.Copy(destRange, copyOptions) method.
  • Copy the row heights and column widths of the source cell range to the destination cell range.
  • Save the workbook to a file using the Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("ContactList.xlsx")

# Get the source worksheet
sheet1 = workbook.Worksheets[0] 
# Get the destination worksheet
sheet2 = workbook.Worksheets[1]

# Get the source cell range
range1 = sheet1.Range["A1:E7"]
# Get the destination cell range
range2 = sheet2.Range["A1:E7"]

# Copy the source cell range from the source worksheet to the destination cell range in the destination worksheet
range1.Copy(range2, CopyRangeOptions.All)

# Copy the row heights and column widths of the source cell range to the destination cell range
for i, row in enumerate(range1.Rows):
    for j, column in enumerate(row.Columns):
        range2.Rows[i].Columns[j].ColumnWidth = column.ColumnWidth
        range2.Rows[i].RowHeight = row.RowHeight

# Save the workbook to a file
workbook.SaveToFile("CopyCells.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Copy Rows, Columns and Cells in Excel

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.

Tables are a powerful formatting tool in Word, allowing you to organize and present data effectively. However, the default table borders may not always align with your document's style and purpose. By selectively changing or removing the borders, you can achieve a variety of visual effects to suit your requirements. In this article, we will explore how to change and remove borders for tables in Word documents in Python using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python. It 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 this tutorial: How to Install Spire.Doc for Python on Windows

Change Borders for a Table in Word in Python

Spire.Doc for Python empowers you to retrieve the borders collection of a table by using the Table.TableFormat.Borders property. Once retrieved, you can access individual borders (like top border, bottom border, left border, right border, horizontal border, and vertical border) from the collection and then modify them by adjusting their line style, width, and color. The detailed steps are as follows.

  • Create an object of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section using Document.Sections[index] property.
  • Get a specific table using Section.Tables[index] property.
  • Get the borders collection of the table using Table.TableFormat.Borders property.
  • Get an individual border, such as the top border from the borders collection using Borders.Top property, and then change its line style, width and color.
  • Refer to the above step to get other individual borders from the borders collection, and then change their line style, width and color.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an object of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Table.docx")

# Add a section to the document
section = document.Sections[0]

# Get the first table in the section
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# Get the collection of the borders
borders = table.TableFormat.Borders

# Get the top border and change border style, line width, and color
topBorder = borders.Top
topBorder.BorderType = BorderStyle.Single
topBorder.LineWidth = 1.0
topBorder.Color = Color.get_YellowGreen()

# Get the left border and change border style, line width, and color
leftBorder = borders.Left
leftBorder.BorderType = BorderStyle.Single
leftBorder.LineWidth = 1.0
leftBorder.Color = Color.get_YellowGreen()

# Get the right border and change border style, line width, and color
rightBorder = borders.Right
rightBorder.BorderType = BorderStyle.Single
rightBorder.LineWidth = 1.0
rightBorder.Color = Color.get_YellowGreen()

# Get the bottom border and change border style, line width, and color
bottomBorder = borders.Bottom
bottomBorder.BorderType = BorderStyle.Single
bottomBorder.LineWidth = 1.0
bottomBorder.Color = Color.get_YellowGreen()

# Get the horizontal border and change border style, line width, and color
horizontalBorder = borders.Horizontal
horizontalBorder.BorderType = BorderStyle.Dot
horizontalBorder.LineWidth = 1.0
horizontalBorder.Color = Color.get_Orange()

# Get the vertical border and change border style, line width, and color
verticalBorder = borders.Vertical
verticalBorder.BorderType = BorderStyle.Dot
verticalBorder.LineWidth = 1.0
verticalBorder.Color = Color.get_CornflowerBlue()

# Save the resulting document
document.SaveToFile("ChangeBorders.docx", FileFormat.Docx2013)
document.Close()

Python: Change or Remove Borders for Tables in Word

Remove Borders from a Table in Word in Python

To remove borders from a table, you need to set the BorderType property of the borders to BorderStyle.none. The detailed steps are as follows.

  • Create an object of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section using Document.Sections[index] property.
  • Get a specific table using Section.Tables[index] property.
  • Get the borders collection of the table using Table.TableFormat.Borders property.
  • Get an individual border, such as the top border from the borders collection using Borders.Top property. Then set the BorderType property of the top border to BorderStyle.none.
  • Refer to the above step to get other individual borders from the borders collection and then set the BorderType property of the borders to BorderStyle.none.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Initialize an instance of the Document class
document = Document()
document.LoadFromFile("ChangeBorders.docx")

# Add a section to the document
section = document.Sections[0]

# Get the first table in the section
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# Get the borders collection of the table
borders = table.TableFormat.Borders

# Remove top border
topBorder = borders.Top
topBorder.BorderType = BorderStyle.none

# Remove left border
leftBorder = borders.Left
leftBorder.BorderType = BorderStyle.none

# Remove right border
rightBorder = borders.Right
rightBorder.BorderType = BorderStyle.none

# Remove bottom border
bottomBorder = borders.Bottom
bottomBorder.BorderType = BorderStyle.none

# remove inside horizontal border
horizontalBorder = borders.Horizontal
horizontalBorder.BorderType = BorderStyle.none

# Remove inside vertical border
verticalBorder = borders.Vertical
verticalBorder.BorderType = BorderStyle.none

# Save the resulting document
document.SaveToFile("RemoveBorders.docx", FileFormat.Docx2013)
document.Close()

Python: Change or Remove Borders for Tables in Word

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.

page 41

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details