Translate Excel using AI

2024-03-06 01:07:22 Written by Koohji

Excel documents, being a widely-used electronic spreadsheet across the globe, often involve multi-language collaboration in various scenarios. Against this backdrop, we introduce AI-powered translation technology, which not only accurately identifies and translates text content but also preserves the original data formats and structures, significantly enhancing work efficiency and accuracy while reducing communication costs incurred by language differences. In this article, we will introduce how to translate Excel using Spire.XLS AI.

Install Spire.XLS for .NET

The Excel AI integrated into Spire.XLS for .NET package, hence to begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Request a License Key of AI Product

A license key is required to run Spire.XLS AI, please contact our sales department (sales@e-iceblue.com) to request one.

Use AI to Translate Excel

Spire.XLS AI provides the ExcelAI class, which supports intelligent translation of either all content within a worksheet or specific ranges within specific worksheets. Below is an overview of the key methods involved in implementing this functionality:

  • UploadWorkbook(Workbook wb): This method is used to upload a Workbook object processed by Spire.XLS to the AI server, facilitating the integration of Excel content with the AI system's data.
  • ExcelTranslate(List<CellRange> srcRanges, string language): This method is designed to translate the content within specified cell ranges from worksheets into the specified target language.
  • ExcelTranslate(List<Worksheet> srcWorksheets, string language, bool translateSheetName = true): This method is responsible for translating all content across a list of source worksheets, optionally including worksheet names, into the specified target language.

Translate Contents from Worksheets in C#

The following steps introduce how to translate the content within worksheets:

  • Create a Workbook class instance.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Create an ExcelAI class instance.
  • Upload the workbook to AI system using ExcelAI.UploadWorkbook() method.
  • Create a list and store all worksheets from workbook.
  • Translate content within worksheets into specified language using ExcelTranslate(List<Worksheet> srcWorksheets, string language, bool translateSheetName = true) method.
  • Save the translated workbook using Workbook.SaveToFile () method.
  • C#
using Spire.Xls;
using Spire.Xls.AI;
using System.Collections.Generic;

// Define the file path of the Excel document 
string inputfile = "Input.xlsx";

// Create a new instance of the Workbook
Workbook wb = new Workbook();

// Load the Excel file
wb.LoadFromFile(inputfile);

// Create a new instance of the ExcelAI
ExcelAI excelAI = new ExcelAI();

// Upload the workbook to AI system
excelAI.UploadWorkbook(wb);

// Create a list to store worksheets
List worksheets = new List();

// Iterate through each worksheet
foreach (Worksheet worksheet in wb.Worksheets)
{
    // Add the worksheet to the list
    worksheets.Add(worksheet);
}

// Translate the content of worksheets into Spanish
excelAI.ExcelTranslate(worksheets, "spanish");

// Save the translated workbook to a new Excel file
wb.SaveToFile("output.xlsx", ExcelVersion.Version2016);

Translate Excel using AI

Translate Contents from Ranges in C#

The following steps introduce how to translate the content within specified ranges:

  • Create a Workbook class instance.
  • Load an Excel document using Workbook.LoadFromFile() method.
  • Create an ExcelAI class instance.
  • Upload the workbook to AI system using ExcelAI.UploadWorkbook() method.
  • Create a list to store cell ranges.
  • Get the specified worksheet and add some cell ranges into the list.
  • Translate content within the ranges into specified language using ExcelTranslate(List<CellRange> srcRanges, string language) method.
  • Save the translated workbook using Workbook.SaveToFile () method.
  • C#
using Spire.Xls;
using Spire.Xls.AI;
using System.Collections.Generic;

// Define the file path of the Excel document 
string inputfile = "Input.xlsx";

// Create a new instance of the Workbook
Workbook wb = new Workbook();

// Load the Excel file
wb.LoadFromFile(inputfile);

// Create a new instance of the ExcelAI
ExcelAI excelAI = new ExcelAI();

// Upload the workbook to AI system
excelAI.UploadWorkbook(wb);

// Get the first worksheet
Worksheet worksheet = wb.Worksheets[0];

// Create a list to store cell ranges
List ranges = new List();

// Add two ranges to the list
ranges.Add(worksheet.Range["B1"]);
ranges.Add(worksheet.Range["H2"]);

// Translate the content of ranges into Chinese
excelAI.ExcelTranslate(ranges, "chinese");

// Save the translated workbook to a new Excel file
wb.SaveToFile("output.xlsx", ExcelVersion.Version2016);

Translate Excel using AI

Cell borders play a crucial role in enhancing the visual clarity and organization of data in Excel spreadsheets. Adding borders to cells can help draw attention to specific data points, highlight headers, or create clear boundaries between different sections of your worksheet. On the other hand, removing borders can provide a sleek and seamless appearance, especially when you want to eliminate distractions and focus solely on the data itself.

In this comprehensive guide, we will walk you through the process of adding or removing cell borders in Excel by using the Spire.XLS for Python library.

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

Add Borders to a Selected Cell or Cell Range in a Worksheet in Python

Borders can be applied to individual cells, groups of cells, or even entire ranges to create clear boundaries and make data stand out. By adding borders, you can effectively organize and structure your data, making it easier to analyze and understand.

With Spire.XLS for Python, accessing specific cells or cell ranges is made easy through the Worksheet.Range[name: str] property. Once you have obtained the desired cell or cell range, you can apply an outside border using the CellRange.BorderAround() method. Additionally, you can apply inside borders to a cell range using the CellRange.BorderInside() method.

To apply borders to a cell or cell range, follow the steps below.

  • Create a Workbook object.
  • Load an Excel document from the specified path.
  • Get a specific worksheet from the workbook.
  • Get a cell or cell range using Worksheet.Range[name: str] property.
  • Apply outside borders to the cell or cell range using CellRange.BorderAround() method.
  • Apply inside borders to the cell range using CellRange.BorderInside() method.
  • Save the workbook to a new Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Get a specific cell 
cell = worksheet.Range["C11"]

# Apply borders to the cell
cell.BorderAround(LineStyleType.Medium, Color.get_Black())

# Get a cell range
cellRange = worksheet.Range["B2:C6"]

# Apply outside borders to the cell range
cellRange.BorderAround(LineStyleType.Thin, Color.get_Black())

# Apply inside borders to the cell range
cellRange.BorderInside(LineStyleType.Thin, Color.get_Black())

# Save the document
workbook.SaveToFile("Output/AddBordersToCellRange.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

Python: Add or Remove Cell Borders in Excel

Add Borders to the Cell Range Containing Data in a Worksheet in Python

The range that contains data in a worksheet is commonly referred to as the "allocated range" or "used range". It represents the rectangular area that encompasses all the cells with data, including text, numbers, formulas, and other types of content.

To retrieve the cell range having data, use the Worksheet.AllocatedRange property. Then, you can easily apply borders to this range using the BorderAround() and BorderInside() methods.

The steps to add borders to the cell range containing data are as follows.

  • Create a Workbook object.
  • Load an Excel document from the specified path.
  • Get a specific worksheet from the workbook.
  • Get the cell range that contains data using Worksheet.AllocatedRange property.
  • Apply outside borders to the cell or cell range using CellRange.BorderAround() method.
  • Apply inside borders to the cell range using CellRange.BorderInside() method.
  • Save the workbook to a new Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Get the cell range that contains data
locatedRange = worksheet.AllocatedRange

# Apply outside borders to the cell range
locatedRange .BorderAround(LineStyleType.MediumDashed, Color.get_Black())

# Apply inside borders to the cell range
locatedRange .BorderInside(LineStyleType.Thin, Color.get_Black())

# Save the document
workbook.SaveToFile("Output/AddBordersToLocatedRange.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

Python: Add or Remove Cell Borders in Excel

Add Left, Top, Right, Bottom, Diagonal Borders to a Cell in Python

In addition to applying outside and inside borders, you have the option to add left, top, right, bottom, and diagonal borders to individual cells or cell ranges. This feature allows you to go beyond basic border customization and provides additional flexibility to highlight important information, separate sections within your worksheet, or provide a visual structure to your data.

Spire.XLS provides convenient access to specific borders, including the left, right, top, bottom, and diagonal borders, through properties such as CellRange.Borders[BordersLineType.EdgeLeft]. Once you have obtained the desired border, you have the flexibility to customize its appearance by utilizing the IBorder.LineStyle property and the IBorder.Color property.

To add left, top, right, bottom, diagonal borders to a cell, follow the following steps.

  • Create a Workbook object.
  • Load an Excel document from the specified path.
  • Get a specific worksheet from the workbook.
  • Get a specific cell using Worksheet.Range property.
  • Get the left, top, right, bottom and diagonal borders of the cell using the properties such as CellRange.Borders[BordersLineType.EdgeLeft].
  • Set the line style of the border using IBorder.LineStyle property
  • Set the color of the border using IBorder.Color property.
  • Save the workbook to a new Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Get a cell
cell = worksheet.Range["B11"]

# Get the left, top, right, bottom border of the cell 
leftBorder = cell.Borders[BordersLineType.EdgeLeft]
topBorder = cell.Borders[BordersLineType.EdgeTop]
rightBorder = cell.Borders[BordersLineType.EdgeRight]
bottomBorder = cell.Borders[BordersLineType.EdgeBottom]

# Set the border type respectively
leftBorder.LineStyle = LineStyleType.Thick
topBorder.LineStyle = LineStyleType.Dotted
rightBorder.LineStyle = LineStyleType.SlantedDashDot
bottomBorder.LineStyle = LineStyleType.Double

# Set the border color respectively
leftBorder.Color = Color.get_Red()
topBorder.Color = Color.get_Brown()
rightBorder.Color = Color.get_DarkGray()
bottomBorder.Color = Color.get_OrangeRed()

# Get a specific cell 
cell = worksheet.Range["C10"]

# Get the diagonal border of the cell
diagonalBorder = cell.Borders[BordersLineType.DiagonalDown]

# Set the border style
diagonalBorder.LineStyle = LineStyleType.Thin

# Save the document
workbook.SaveToFile("Output/BorderOfEdge.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

Python: Add or Remove Cell Borders in Excel

Remove Borders of a Cell or Cell Range in a Worksheet in Python

Borders can be removed from individual cells, groups of cells, or even entire ranges, allowing you to reduce visual noise and clutter, making your data easier to interpret and analyze. Additionally, you can choose to remove borders from specific sides of a cell, such as the left, top, right, or bottom, which can alter the visual appearance and enhance the overall presentation.

To eliminate borders surrounding or within a cell or cell range, you can easily achieve this by setting the CellRange.Borders.LineStyle property to LineStyleType.none. Similarly, if you want to remove a border on a specific side, such as the left side, you can accomplish this by setting the CellRange.Borders[BordersLineType.EdgeLeft].LineStyle property to LineStyleType.none.

The steps to remove borders of a cell or cell range as follows.

  • Create a Workbook object.
  • Load an Excel document from the specified path.
  • Get a specific worksheet from the workbook.
  • Get a specific cell or cell range using Worksheet.Range property.
  • Remove all borders of the cell or cell range by setting CellRange.Borders.LineStyle property to LineStyleType.none.
  • Save the workbook to a new Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\BorderExample.xlsx")

# Get the first worksheet
worksheet = workbook.Worksheets[0]

# Get a cell 
cell = worksheet.Range["C11"]

# Remove borders by setting line style to none
cell.Borders.LineStyle = LineStyleType.none

# Remove border on a specific side
# cell.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.none

# Get a cell range
cellRange = worksheet.Range["B2:C6"]

# Remove borders by setting line style to none
cellRange.Borders.LineStyle = LineStyleType.none

# Save the document
workbook.SaveToFile("Output/RemoveBorders.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

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.

Gridlines in Microsoft Excel provide a visual aid that helps users navigate through data and maintain a structured layout. By default, Excel displays gridlines in a light color to separate cells, making it easier to distinguish and locate specific data. However, there are instances when you may want to hide or even modify the appearance of gridlines to suit your specific needs. In this article, we will explore how to hide, show, and change gridlines 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

Hide or Show Gridlines in Excel in Python

The Worksheet.GridLinesVisible property provided by Spire.XLS for Python is used to control the visibility of gridlines in an Excel worksheet. If you want to hide the gridlines in the worksheet, set this property to False. Conversely, if you wish to make the hidden gridlines visible again, set this property to True. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index using the Workbook.Worksheets[index] property.
  • Hide or show the gridlines in the worksheet by setting the Worksheet.GridLinesVisible property as False or True.
  • Save the result 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("Input.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]
            
# Hide the gridlines in the worksheet
sheet.GridLinesVisible = False
 
# Show the hidden gridlines in the worksheet
# sheet.GridLinesVisible = True
 
# Save the result file
workbook.SaveToFile("HideGridlines.xlsx", ExcelVersion.Version2016)

workbook.Dispose()

Python: Hide, Show, or Change Gridlines in Excel

Change Gridlines in Excel in Python

Spire.XLS for Python provides the Worksheet.GridLineColor property, which allows you to customize the color of gridlines in an Excel worksheet. By using this property, you can change the default color of gridlines to your desired choice. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an Excel file using the Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index using the Workbook.Worksheets[index] property.
  • Change the color of the gridlines in the worksheet using the Worksheet.GridLineColor property.
  • Save the result 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("Input.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Change the color of the gridlines in the worksheet
sheet.GridLineColor = ExcelColors.Red
 
# Save the result file
workbook.SaveToFile("ChangeGridlineColor.xlsx", ExcelVersion.Version2016)

workbook.Dispose()

Python: Hide, Show, or Change Gridlines 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.

page 69