openpyxl
  1. openpyxl-setting-cell-values-and-styles

Setting Cell Values and Styles - ( Cell Formatting in Openpyxl )

Heading h2

Syntax

# Set the value of a cell
worksheet.cell(row, column, value)

# Set the font style of a cell
from openpyxl.styles import Font
font = Font(size=12, bold=True)
worksheet.cell(row, column).font = font

# Set the fill color of a cell
from openpyxl.styles import PatternFill
fill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
worksheet.cell(row, column).fill = fill

# Set the border of a cell
from openpyxl.styles import Border, Side
border = Border(left=Side(border_style='thin', color='000000'),
                right=Side(border_style='thin', color='000000'),
                top=Side(border_style='thin', color='000000'),
                bottom=Side(border_style='thin', color='000000'))
worksheet.cell(row, column).border = border

Example

import openpyxl
from openpyxl.styles import Font, PatternFill, Border, Side

# Load workbook and select worksheet
workbook = openpyxl.Workbook()
worksheet = workbook.active

# Set the value of a cell
worksheet.cell(1, 1, 'Name')

# Set the font style of a cell
font = Font(size=16, bold=True)
worksheet.cell(1, 1).font = font

# Set the fill color of a cell
fill = PatternFill(start_color='FFC000', end_color='FFC000', fill_type='solid')
worksheet.cell(1, 1).fill = fill

# Set the border of a cell
border = Border(left=Side(border_style='thin', color='000000'),
                right=Side(border_style='thin', color='000000'),
                top=Side(border_style='thin', color='000000'),
                bottom=Side(border_style='thin', color='000000'))
worksheet.cell(1, 1).border = border

# Save the workbook
workbook.save('example.xlsx')

Output

The output will be an Excel file named 'example.xlsx' with a single cell ('Name') that is formatted with a specific font style, fill color, and border.

Explanation

Cell formatting is an important part of working with Excel spreadsheets since it helps to make the data more visually appealing and easier to read. Openpyxl is a Python library that allows you to manipulate Excel spreadsheets and provides several tools for cell formatting.

In the above example, we create a new workbook and select a worksheet to work with. We then set the value of a cell, apply various formatting styles to it, and finally save the workbook.

Use

Cell formatting is useful in various applications where we need to present data to end-users in a visually appealing and understandable format. It can be used in financial reports, data analysis, scientific research papers, and many more.

Important Points

  • Cell formatting is an important part of working with Excel spreadsheets
  • Openpyxl provides several tools for cell formatting, such as font styles, fill colors, and borders.
  • Cell formatting can make the data more visually appealing and easier to read.

Summary

In conclusion, cell formatting is an important aspect of working with Excel spreadsheets and Openpyxl provides several tools to help you achieve this. Proper formatting can make the data more visually appealing and easier to read. The above example demonstrates how to set cell values and formatting styles using Openpyxl.

Published on: