Spire.Office Knowledgebase Page 28 | E-iceblue

In today's digital world, extracting text from images has become essential for many fields, including business, education, and data analysis. OCR (Optical Character Recognition) technology makes this process effortless by converting text in images into editable and searchable formats quickly and accurately. Whether it's turning handwritten notes into digital files or pulling key information from scanned documents, OCR simplifies tasks and makes work more efficient. In this article, we will demonstrate how to recognize text from images in Python using Spire.OCR for Python.

Install Spire.OCR for Python

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

pip install Spire.OCR

Download the Model of Spire.OCR for Python

Spire.OCR for Python provides different recognition models for different operating systems. Download the model suited to your system from one of the links below:

After downloading, extract the package and save it to a specific directory on your system.

Recognize Text from Images in Python

Spire.OCR for Python offers the OcrScanner.Scan() method to recognize text from images. Once the recognition is complete, you can use the OcrScanner.Text property to retrieve the recognized text and then save it to a file for further use. The detailed steps are as follows.

  • Create an instance of the OcrScanner class to handle OCR operations.
  • Create an instance of the ConfigureOptions class to configure the OCR settings.
  • Specify the file path to the model and the desired recognition language through the ConfigureOptions.ModelPath and ConfigureOptions.Language properties.
  • Apply the configuration settings to the OcrScanner instance using the OcrScanner.ConfigureDependencies() method.
  • Call the OcrScanner.Scan() method to perform text recognition on the image.
  • Retrieve the recognized text using the OcrScanner.Text property.
  • Save the extracted text to a file for further use.
  • Python
from spire.ocr import *

# Create an instance of the OcrScanner class
scanner = OcrScanner()

# Configure OCR settings
configureOptions = ConfigureOptions()
# Set the file path to the model
configureOptions.ModelPath = r'D:\OCR\win-x64'  
# Set the recognition language. Supported languages include English, Chinese, Chinesetraditional, French, German, Japanese, and Korean.
configureOptions.Language = 'English'  
# Apply the settings to the OcrScanner instance
scanner.ConfigureDependencies(configureOptions)

# Recognize text from the image
scanner.Scan(r'Sample.png')

# Retrieve the recognized text and save it to a file
text = scanner.Text.ToString() + '\n'
with open('output.txt', 'a', encoding='utf-8') as file:
    file.write(text + '\n')

Recognize Text from Images in Python

Recognize Text with Coordinates from Images in Python

In scenarios where you need the exact position of text in an image, such as for layout analysis or advanced data processing, extracting coordinate information is essential. With Spire.OCR for Python, you can retrieve recognized text block by block. Each text block includes detailed positional data such as the x and y coordinates, width, and height. The detailed steps are as follows.

  • Create an instance of the OcrScanner class to handle OCR operations.
  • Create an instance of the ConfigureOptions class to configure the OCR settings.
  • Specify the file path to the model and the desired recognition language through the ConfigureOptions.ModelPath and ConfigureOptions.Language properties.
  • Apply the configuration settings to the OcrScanner instance using the OcrScanner.ConfigureDependencies() method.
  • Call the OcrScanner.Scan() method to perform text recognition on the image.
  • Retrieve the recognized text using the OcrScanner.Text property.
  • Iterate through the text blocks in the recognized text. For each block, use the IOCRTextBlock.Text property to get the text and the IOCRTextBlock.Box property to retrieve positional details (x, y, width, and height).
  • Save the results to a text file for further analysis.
  • Python
from spire.ocr import *

# Create an instance of the OcrScanner class
scanner = OcrScanner()

# Configure OCR settings
configureOptions = ConfigureOptions()
# Set the file path to the model
configureOptions.ModelPath = r'D:\OCR\win-x64' 
# Set the recognition language. Supported languages include English, Chinese, Chinesetraditional, French, German, Japanese, and Korean.
configureOptions.Language = 'English' 
# Apply the settings to the OcrScanner instance
scanner.ConfigureDependencies(configureOptions)

# Recognize text from the image
scanner.Scan(r'sample.png')
# Retrieve the recognized text 
text = scanner.Text

# Iterate through the text blocks in the recognized text. For each text block, retrieve its text and positional data (x, y, width, and height)
block_text = ""
for block in text.Blocks:
    rectangle = block.Box
    block_info = f'{block.Text} -> x: {rectangle.X}, y: {rectangle.Y}, w: {rectangle.Width}, h: {rectangle.Height}'
    block_text += block_info + '\n'

# Save the results to a file
with open('output.txt', 'a', encoding='utf-8') as file:
    file.write(block_text + '\n')

Recognize Text with Coordinates from Images in Python

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.

In Excel file processing, using byte streams in Python to create, read, and modify Excel files enables efficient data manipulation and automation. This approach eliminates reliance on physical storage or local filesystems, making it ideal for cloud-based or memory-constrained environments. It also supports real-time data exchange, system integration, and instant feedback in web applications, promoting rapid development and adaptable workflows. In this article, we will explore how to use Spire.XLS for Python to dynamically process Excel workbooks by byte streams with simple Python code.

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 commands.

pip install Spire.XLS

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

Create Excel Files and Save as Byte Streams in Python

With Spire.XLS for Python, we can create an Excel workbook by initializing a Workbook instance and populating it with data. Once the workbook is ready, we can save it to a Stream object and convert that stream into a bytes object for further use or storage. This method allows us to efficiently generate Excel files in memory without the need for disk storage.

Below are the steps for creating an Excel file and saving it as a byte stream with Python:

  • Create an instance of the Workbook class to initialize a new Excel workbook. The new workbook includes three default worksheets.
  • Retrieve a worksheet using the Workbook.Worksheets.get_Item() method.
  • Create a data list or obtain it from another source.
  • Iterate through rows and columns to populate the worksheet with data using the Worksheet.Range.get_Item().Value or NumberValue properties.
  • Format cells using the properties available in CellRange.Style.
  • Create a Stream object and save the workbook to it using the Workbook.SaveToStream() method.
  • Convert the stream to a bytes object using the Stream.ToArray() method.
  • Python
from spire.xls import Workbook, FileFormat, Stream, Color, HorizontalAlignType

# Create an instance of Workbook class
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)

# Create a 2D list of data or read data from other sources
data = [
    ["Country", "Capital", "Population (Millions)", "Area (km²)", "Continent"],
    ["United States", "Washington, D.C.", 331, 9833520, "North America"],
    ["Canada", "Ottawa", 38, 9984670, "North America"],
    ["Brazil", "Brasília", 213, 8515767, "South America"],
    ["United Kingdom", "London", 68, 243610, "Europe"],
    ["Germany", "Berlin", 83, 357022, "Europe"],
    ["India", "New Delhi", 1391, 3287263, "Asia"],
    ["China", "Beijing", 1441, 9596961, "Asia"],
    ["Australia", "Canberra", 26, 7692024, "Oceania"],
    ["South Africa", "Pretoria", 60, 1219090, "Africa"],
    ["Japan", "Tokyo", 126, 377975, "Asia"]
]

# Insert the data into the worksheet
for i, row in enumerate(data):
    for j, value in enumerate(row):
        if isinstance(value, str):
            sheet.Range.get_Item(i + 1, j + 1).Value = value
        else:
            sheet.Range.get_Item(i + 1, j + 1).NumberValue = value

# Format the header row with new colors
headerRow = sheet.AllocatedRange.Rows.get_Item(0)
headerRow.Style.Color = Color.FromRgb(0, 102, 204)  # Blue color for the header
headerRow.Style.Font.FontName = "Calibri"
headerRow.Style.Font.Size = 14
headerRow.Style.Font.IsBold = True
headerRow.Style.Font.Color = Color.FromRgb(255, 255, 255)  # White text
headerRow.Style.HorizontalAlignment = HorizontalAlignType.Center

# Format the data rows with new alternating colors
for i in range(1, sheet.AllocatedRange.Rows.Count):
    row = sheet.AllocatedRange.Rows.get_Item(i)
    row.Style.Font.FontName = "Times New Roman"
    row.Style.Font.Size = 12
    row.Style.HorizontalAlignment = HorizontalAlignType.Left
    if i % 2 == 0:
        row.Style.Color = Color.FromRgb(229, 243, 255)  # Light blue for even rows
    else:
        row.Style.Color = Color.FromRgb(255, 255, 204)  # Light yellow for odd rows

# Auto-fit the columns
for i in range(sheet.AllocatedRange.Columns.Count):
    sheet.AutoFitColumn(i + 1)

# Create a Stream object
stream = Stream()

# Save the workbook to the stream
workbook.SaveToStream(stream, FileFormat.Version2016)
workbook.Dispose()

# Convert the stream to bytes
bytes_data = stream.ToArray()

# Write the bytes to a file or use them as needed
with open("output/CreateExcelByStream.xlsx", "wb") as file:
    file.write(bytes_data)

Excel Files Created with Python Through Byte Streams

Read Excel Files from Byte Streams in Python

To load an Excel workbook from a byte stream, we can convert the byte data into a Stream object and load it into a Workbook instance. Then, we can then access the worksheet data to extract and utilize the data within the Python application seamlessly.

The steps for reading Excel files from byte streams using Python are as follows:

  • Create or convert to a bytes object for the Excel file, or use an existing one.
  • Create a Stream object from the bytes.
  • Instantiate the Workbook class and load the Excel file from the Stream object using the Workbook.LoadFromStream() method.
  • Retrieve a worksheet using the Workbook.Worksheets.get_Item() method.
  • Iterate through rows and columns to access cell values using the Worksheet.AllocatedRange.get_Item().Value property.
  • Output the values or utilize them as needed.
  • Python
from spire.xls import Workbook, Stream

# Create a bytes object or use an existing one
with open("output/CreateExcelByStream.xlsx", "rb") as file:
    bytes_data = file.read()

# Create an instance of the Workbook class
workbook = Workbook()

# Load the Excel file from the byte stream
workbook.LoadFromStream(Stream(bytes_data))

# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)

# Read data from the worksheet
# Create a list to store the data
data = []
for i in range(sheet.AllocatedRange.Rows.Count):
    # Retrieve a row of data
    row = sheet.AllocatedRange.Rows.get_Item(i)
    # Create a list to store the row's data
    row_data = []
    for j in range(row.Cells.Count):
        # Get the value of the cell
        cellValue = sheet.AllocatedRange.get_Item(i + 1, j + 1).Value
        row_data.append(cellValue)
    data.append(row_data)

# Display the data or use it as needed
for row in data:
    print(row)

# Release resources
workbook.Dispose()

Data Read from Excel Streams with Spire.XLS

Modify Excel Files from Byte Streams in Python

Modifying Excel files from byte streams enables us to update or enhance data dynamically without saving it to disk. This method involves loading the byte stream into a Workbook instance, making changes to its content or formatting, and saving the changes back to a byte stream for reuse.

The following steps show how to modify an Excel workbook from a byte stream using Python:

  • Create or convert to a bytes object of the Excel file, or use an existing one.
  • Initialize a Stream object from the bytes and load it into a Workbook using the Workbook.LoadFromStream() method.
  • Access a worksheet using the Workbook.Worksheets.get_Item() method.
  • Modify cell values with the Worksheet.AllocatedRange.get_Item().Value property.
  • Format cells using properties in CellRange.Style and add borders with the CellRange.BorderAround() method or the CellRange.BorderInside() method.
  • Auto-fit column widths using the Worksheet.AutoFitColumn() method.
  • Save the workbook to a new Stream object using the Workbook.SaveToStream() method and convert it back to bytes or bytearray using Stream.ToArray() method.
  • Python
from spire.xls import Workbook, Stream, HorizontalAlignType, Color, FileFormat

# Create a bytes object or use an existing one
with open("output/CreateExcelByStream.xlsx", "rb") as file:
    bytes_data = file.read()

# Create an instance of the Workbook class
workbook = Workbook()

# Load the Excel file from the byte stream
stream = Stream(bytes_data)
workbook.LoadFromStream(stream)
stream.Close()

# Remove unnecessary worksheets (commented out in this case)
#for i in range(1, workbook.Worksheets.Count):
#    workbook.Worksheets.RemoveAt(i)

# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)

# Modify the style of the header row
headerRow = sheet.AllocatedRange.Rows.get_Item(0)
headerRow.Style.Font.Bold = False
headerRow.Style.Font.FontName = "Arial"
headerRow.Style.Font.Size = 12
headerRow.Style.HorizontalAlignment = HorizontalAlignType.Left
headerRow.Style.Color = Color.FromRgb(173, 216, 230) # Light blue background color
# Add outline borders for the header row
headerRow.BorderAround()

# Modify the style of the data rows
for i in range(1, sheet.AllocatedRange.Rows.Count):
    row = sheet.AllocatedRange.Rows.get_Item(i)
    row.Style.Font.FontName = "Consolas"
    row.Style.Font.Size = 11
    if i % 2 == 0:
        row.Style.Color = Color.FromRgb(240, 240, 240)  # Light gray background color for even rows
    else:
        row.Style.Color = Color.FromRgb(255, 255, 255)  # White background color for odd rows

# Auto-adjust the column widths
for i in range(sheet.AllocatedRange.Columns.Count):
    sheet.AutoFitColumn(i + 1)

# Save the modified Excel file
streamTemp = Stream()
workbook.SaveToStream(streamTemp, FileFormat.Version2016)
workbook.Dispose()

# Convert the stream to bytes
bytes_data = streamTemp.ToArray()

# Write the bytes to a file or use them as needed
with open("output/ModifiedExcel.xlsx", "wb") as file:
    file.write(bytes_data)

Modify Excel Worksheets with Python in Streams

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.

Spire.OCR for Python is a robust and professional Optical Character Recognition (OCR) library that enables developers to extract text from images in various formats, including JPG, PNG, GIF, BMP, and TIFF. It provides an intuitive and straightforward solution for integrating OCR capabilities into Python applications, allowing users to extract text from popular image formats with just a few lines of code.

The library supports text recognition in commonly used fonts like Arial, Times New Roman, Courier New, Verdana, Tahoma, and Calibri, with regular, bold, and italic text styles. It also enables developers to recognize text in multiple languages, including English, Chinese, French, German, Japanese, and Korean, making it a versatile tool for global applications.

page 28