IRONSOFTWAREHOME
样式Excel单元格边框和字体
from ironxl import *

# Load existing spreadsheet
workbook = WorkBook.Load("sample.xls")
worksheet = workbook.WorkSheets[0]

# Get a range from an Excel worksheet
selected_range = worksheet["A1:H10"]

# Get the first cell in the range
cell = selected_range.ToArray()[0]

# Set background color of the cell with an RGB string
cell.Style.SetBackgroundColor("#428D65")

# Apply styling to the whole range

# Set underline property to the font
# FontUnderlineType is an enum that stands for different types of font underline
selected_range.Style.Font.Underline = FontUnderlineType.SingleAccounting

# Define whether to use a horizontal line through the text or not
selected_range.Style.Font.Strikeout = False

# Define whether the font is bold or not
selected_range.Style.Font.Bold = True

# Define whether the font is italic or not
selected_range.Style.Font.Italic = False

# Get or set the script property of a font
# FontScript enum stands for available options
selected_range.Style.Font.FontScript = FontScript.Super

# Set the type of the border line
# There are also TopBorder, LeftBorder, RightBorder, DiagonalBorder properties
# BorderType enum indicates the line style of a border in a cell
selected_range.Style.BottomBorder.Type = BorderType.MediumDashed

# Indicate whether the cell should be auto-sized
selected_range.Style.ShrinkToFit = True

# Set alignment of the cell
selected_range.Style.VerticalAlignment = VerticalAlignment.Bottom

# Set border color
selected_range.Style.DiagonalBorder.SetColor("#20C96F")

# Define border type and border direction as well
selected_range.Style.DiagonalBorder.Type = BorderType.Thick

# DiagonalBorderDirection enum stands for direction of diagonal border inside cell
selected_range.Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward

# Set background color of cells
selected_range.Style.SetBackgroundColor(Color.Aquamarine)

# Set fill pattern of the cell
# FillPattern enum indicates the style of fill pattern
selected_range.Style.FillPattern = FillPattern.Diamonds

# Set the number of spaces to indent the text
selected_range.Style.Indention = 5

# Indicate if the text is wrapped
selected_range.Style.WrapText = True

# Save changes with applied styling options
workbook.SaveAs("stylingOptions.xls")
pip install ironxl
样式Excel单元格边框和字体

样式Excel单元格边框和字体

IronXL for Python允许开发者对任何Excel CellRange进行样式设置。 IronXL涵盖了Microsoft Excel中大部分可用的样式,包括字体、文本对齐、边框、颜色、背景和填充模式。 下面的代码示例演示了可以通过IronXL配置的多种样式。

单个单元格和范围的边框(底部、顶部、左侧、右侧和对角线)可以使用BorderType枚举设置为14种不同的边框样式,例如虚线、短划线和双线。 ShrinkToFitWrapText属性提供了对电子表格如何管理单元格内容和大小的额外控制。

学习如何在Python中调整Excel单元格字体大小

准备开始了吗?

版本:2026.9刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户