杰瑞科技汇

python xlwt borders

Of course! Working with borders in the xlwt library is a common requirement for creating professional-looking Excel files. The key is to use xlwt's easyxf style object.

python xlwt borders-图1
(图片来源网络,侵删)

Here’s a complete guide, from basic concepts to practical examples.

The Core Concept: easyxf

In xlwt, formatting is handled through easyxf (which stands for "easy XF" or "extended format"). You create an easyxf object, apply styles to it (like font, alignment, and borders), and then apply that style to a cell or a range of cells.

To create borders, you define the borders attribute within the easyxf constructor.

Border Styles

You have two main parts to define a border style:

python xlwt borders-图2
(图片来源网络,侵删)
  1. Border Type: The appearance of the line (thin, thick, dashed, etc.).
  2. Color: The color of the line.

Border Types

You can set different styles for each side (left, right, top, bottom) or set them all at once using all.

  • xlwt.Borders.THIN (default)
  • xlwt.Borders.MEDIUM
  • xlwt.Borders.THICK
  • xlwt.Borders.DASHED
  • xlwt.Borders.DOTTED
  • xlwt.Borders.DOUBLE
  • xlwt.Borders.HAIR
  • xlwt.Borders.NONE (no border)

Border Colors

Colors are specified as a tuple of (Red, Green, Blue) values, where each component is an integer from 0 to 255.

  • xlwt.Style.colour_map['black']
  • xlwt.Style.colour_map['red']
  • xlwt.Style.colour_map['green']
  • xlwt.Style.colour_map['blue']
  • Or a custom tuple: (128, 0, 128) for purple.

Basic Examples

Let's start with some simple examples.

Example 1: A Thin Black Border Around a Single Cell

This is the most common use case.

python xlwt borders-图3
(图片来源网络,侵删)
import xlwt
# 1. Create a new workbook and add a sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
# 2. Define a style with a thin black border
style_thin_black = xlwt.easyxf(
    'borders: thin black;'
)
# 3. Write to a cell using the style
sheet.write(0, 0, 'Data with Border', style_thin_black)
# 4. Save the workbook
workbook.save('border_example_1.xls')

Example 2: Different Border Styles on Each Side

You can specify the style for each border individually using a comma-separated list.

import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
# Style with thick top, thin bottom, dashed left, and dotted right
style_custom = xlwt.easyxf(
    'borders: top thick, bottom thin, left dashed, right dotted;'
)
sheet.write(1, 0, 'Custom Borders', style_custom)
workbook.save('border_example_2.xls')

Example 3: Using Custom Colors

Here, we'll create a style with a thick blue border.

import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
# Get the RGB tuple for blue from xlwt's color map
blue_color = xlwt.Style.colour_map['blue']
# Style with a thick blue border
style_thick_blue = xlwt.easyxf(
    f'borders: thick {blue_color};'
)
sheet.write(2, 0, 'Thick Blue Border', style_thick_blue)
workbook.save('border_example_3.xls')

Applying Styles to a Range of Cells (Merging)

To apply a border to a range of cells, you write to the top-left cell of the range and then use the write_merge() method.

Important: The style is applied to the merged cell, not the individual cells it covers. This means the border will be around the entire merged block.

import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
# Style for the merged cell
style_merged = xlwt.easyxf(
    'borders: double red;'
)
# Write the text and merge cells from (row 3, col 0) to (row 5, col 2)
sheet.write_merge(3, 5, 0, 2, 'Merged Cell with Double Red Border', style_merged)
workbook.save('border_example_merge.xls')

Complete, Practical Example

This example combines borders with other styles like font and alignment to create a nicely formatted header row.

import xlwt
# Create workbook and sheet
wb = xlwt.Workbook()
ws = wb.add_sheet('Sales Report')
# --- Define Styles ---
# Style 1: Header with thick borders, bold font, and background color
header_style = xlwt.easyxf(
    'font: bold on, name Arial; '  # Bold Arial font
    'pattern: pattern solid, fore_colour light_blue;'  # Light blue background
    'borders: thick black;'  # Thick black border on all sides
    'alignment: horizontal center;'  # Center align text
)
# Style 2: Data with thin borders
data_style = xlwt.easyxf(
    'borders: thin black;'
)
# --- Write Data ---
# Write headers using the header style
ws.write(0, 0, 'Product', header_style)
ws.write(0, 1, 'Q1 Sales', header_style)
ws.write(0, 2, 'Q2 Sales', header_style)
ws.write(0, 3, 'Total', header_style)
# Write data using the data style
ws.write(1, 0, 'Laptop', data_style)
ws.write(1, 1, '150', data_style)
ws.write(1, 2, '200', data_style)
ws.write(1, 3, '350', data_style)
ws.write(2, 0, 'Mouse', data_style)
ws.write(2, 1, '300', data_style)
ws.write(2, 2, '400', data_style)
ws.write(2, 3, '700', data_style)
# --- Save the file ---
wb.save('sales_report_formatted.xls')
print("Excel file 'sales_report_formatted.xls' created successfully.")

Summary and Key Takeaways

  1. Use xlwt.easyxf(): This is your main tool for creating styles.
  2. String-based Syntax: The easiest way to define styles is with a string like 'borders: thin black; font: bold on;'.
  3. Border Types: Use constants like xlwt.Borders.THIN, xlwt.Borders.THICK, etc.
  4. Border Colors: Use xlwt.Style.colour_map['color_name'] or an RGB tuple like (R, G, B).
  5. Apply to Cells: Pass the easyxf object as the third argument to sheet.write(row, col, value, style).
  6. Merging: Use sheet.write_merge() to apply a style (including borders) to a block of cells. The style defines the border of the entire merged block.
分享:
扫描分享到社交APP
上一篇
下一篇