openpyxl
  1. openpyxl-applying-font-and-alignment

Applying Font and Alignment - ( Cell Formatting in Openpyxl )

Heading h2

Syntax

# Import necessary modules
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment

# Create a new workbook object
workbook = Workbook()

# Select desired sheet
sheet = workbook.active

# Access cell and apply font and alignment
cell = sheet.cell(row=1, column=1)
cell.value = 'Hello World'
cell.font = Font(size=14, italic=True, bold=True, color='FF0000')
cell.alignment = Alignment(horizontal='center', vertical='center')

Example

# Import necessary modules
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment

# Create a new workbook object
workbook = Workbook()

# Select desired sheet
sheet = workbook.active

# Add headers to the sheet
sheet.cell(row=1, column=1).value = 'Name'
sheet.cell(row=1, column=2).value = 'Age'
sheet.cell(row=1, column=3).value = 'Occupation'

# Apply font and alignment to the headers
header_font = Font(size=14, bold=True)
header_alignment = Alignment(horizontal='center', vertical='center')
for col in range(1, 4):
    cell = sheet.cell(row=1, column=col)
    cell.font = header_font
    cell.alignment = header_alignment

# Add data to the sheet
sheet.cell(row=2, column=1).value = 'John Doe'
sheet.cell(row=2, column=2).value = 25
sheet.cell(row=2, column=3).value = 'Software Engineer'

# Apply font and alignment to the data
data_font = Font(size=12)
data_alignment = Alignment(horizontal='left', vertical='center')
for col in range(1, 4):
    cell = sheet.cell(row=2, column=col)
    cell.font = data_font
    cell.alignment = data_alignment

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

Output

The output will be an Excel file named 'formatted_data.xlsx' with formatted cell data.

Explanation

Cell formatting is an important aspect of working with Excel files. Openpyxl provides various tools and techniques for formatting cells in Excel sheets. In the above example, we demonstrate how to apply font and alignment to cells in an Excel sheet.

We start by creating a new workbook object and selecting the desired sheet. We then access the cell we want to format and apply the desired font and alignment to it. We repeat this process for all the cells we want to format.

Use

Cell formatting in Openpyxl can be used to create visually appealing Excel files with organized and easy-to-read data. It can help improve the readability and usability of the Excel files and make them more accessible to end-users.

Important Points

  • Cell formatting is an important aspect of Excel files
  • Openpyxl provides various tools and techniques for formatting cells in Excel sheets
  • Font and alignment are two important formatting attributes of an Excel cell
  • Font helps change the appearance of the text in the cell, while alignment helps change the position of the text within the cell

Summary

In conclusion, cell formatting is an essential aspect of working with Excel files. Openpyxl provides various tools and techniques for formatting cells in Excel sheets, including font and alignment. Applying font and alignment can help improve the readability and usability of Excel files and make them more visually appealing.

Published on: