CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xlsxwriter

A Python module for creating Excel XLSX files.

Overview
Eval results
Files

page-setup.mddocs/

Page Setup and Printing

Complete page setup and print configuration including orientation, margins, headers/footers, page breaks, print areas, scaling options, and paper size settings. These features control how worksheets appear when printed or viewed in page layout mode.

Capabilities

Page Orientation and View

Configure page orientation and view modes.

def set_landscape(self):
    """Set page orientation to landscape."""

def set_portrait(self):
    """Set page orientation to portrait (default)."""

def set_page_view(self, view=1):
    """
    Set the page view mode.
    
    Args:
        view (int): View mode:
            - 0: Normal view
            - 1: Page Layout view
            - 2: Page Break Preview
    """

def set_pagebreak_view(self):
    """Set the worksheet to Page Break Preview mode."""

Paper Size and Scaling

Configure paper size and print scaling options.

def set_paper(self, paper_size):
    """
    Set the paper size for printing.
    
    Args:
        paper_size (int): Paper size code:
            - 1: Letter (8.5 x 11 in)
            - 5: Legal (8.5 x 14 in)
            - 9: A4 (210 x 297 mm)
            - 11: A5 (148 x 210 mm)
            - 20: Envelope #10 (4.125 x 9.5 in)
            - And many other standard paper sizes
    """

def set_print_scale(self, scale):
    """
    Set the print scale percentage.
    
    Args:
        scale (int): Scale percentage (10-400)
        
    Note: Cannot be used with fit_to_pages()
    """

def fit_to_pages(self, width, height):
    """
    Fit the worksheet to a specific number of pages.
    
    Args:
        width (int): Number of pages wide (0 for no limit)
        height (int): Number of pages tall (0 for no limit)
        
    Note: Cannot be used with set_print_scale()
    """

Page Margins

Configure page margins for printing.

def set_margins(self, left=0.7, right=0.7, top=0.75, bottom=0.75):
    """
    Set page margins in inches.
    
    Args:
        left (float): Left margin in inches
        right (float): Right margin in inches
        top (float): Top margin in inches
        bottom (float): Bottom margin in inches
    """

Page Centering

Center content on the printed page.

def center_horizontally(self):
    """Center the worksheet data horizontally on the page."""

def center_vertically(self):
    """Center the worksheet data vertically on the page."""

Headers and Footers

Configure page headers and footers with dynamic content.

def set_header(self, header="", options=None, margin=None):
    """
    Set the page header.
    
    Args:
        header (str): Header text with optional formatting codes:
            - &L: Left section
            - &C: Center section  
            - &R: Right section
            - &P: Page number
            - &N: Total pages
            - &D: Current date
            - &T: Current time
            - &F: File name
            - &Z: Worksheet name
            - &A: Workbook name
            - &"font,size": Font formatting
            - &G: Insert image
        options (dict, optional): Header options:
            - margin (float): Header margin in inches
            - image_left (str): Left section image file
            - image_center (str): Center section image file
            - image_right (str): Right section image file
            - image_data_left (bytes): Left section image data
            - image_data_center (bytes): Center section image data
            - image_data_right (bytes): Right section image data
        margin (float, optional): Header margin (deprecated, use options)
    """

def set_footer(self, footer="", options=None, margin=None):
    """
    Set the page footer.
    
    Args:
        footer (str): Footer text with same formatting codes as header
        options (dict, optional): Same options as set_header
        margin (float, optional): Footer margin (deprecated, use options)
    """

Print Areas and Titles

Define what gets printed and repeated on each page.

def print_area(self, first_row, first_col, last_row, last_col):
    """
    Set the print area for the worksheet.
    
    Args:
        first_row (int): First row of print area (0-indexed)
        first_col (int): First column of print area (0-indexed)
        last_row (int): Last row of print area (0-indexed)
        last_col (int): Last column of print area (0-indexed)
    """

def repeat_rows(self, first_row, last_row=None):
    """
    Set rows to repeat at the top of each printed page.
    
    Args:
        first_row (int): First row to repeat (0-indexed)
        last_row (int, optional): Last row to repeat (defaults to first_row)
    """

def repeat_columns(self, first_col, last_col=None):
    """
    Set columns to repeat at the left of each printed page.
    
    Args:
        first_col (int): First column to repeat (0-indexed)
        last_col (int, optional): Last column to repeat (defaults to first_col)
    """

Print Options

Configure various print display options.

def hide_gridlines(self, option=1):
    """
    Hide gridlines in the printed output.
    
    Args:
        option (int): Gridline hiding option:
            - 0: Show gridlines (default Excel behavior)
            - 1: Hide printed gridlines
            - 2: Hide screen and printed gridlines
    """

def print_row_col_headers(self):
    """Print row and column headers (A, B, C... and 1, 2, 3...)."""

def hide_row_col_headers(self):
    """Hide row and column headers in printed output."""

def print_black_and_white(self):
    """Print in black and white."""

def print_across(self):
    """
    Set page order to print across then down.
    Default is down then across.
    """

Page Breaks

Manually control where page breaks occur.

def set_h_pagebreaks(self, breaks):
    """
    Set horizontal page breaks.
    
    Args:
        breaks (list): List of row numbers (0-indexed) where breaks occur
    """

def set_v_pagebreaks(self, breaks):
    """
    Set vertical page breaks.
    
    Args:
        breaks (list): List of column numbers (0-indexed) where breaks occur
    """

Print Start Page

Set the starting page number for printing.

def set_start_page(self, start_page):
    """
    Set the starting page number.
    
    Args:
        start_page (int): Starting page number (default 1)
    """

Usage Examples

Basic Page Setup

import xlsxwriter

workbook = xlsxwriter.Workbook('page_setup.xlsx')
worksheet = workbook.add_worksheet()

# Set orientation and paper size
worksheet.set_landscape()
worksheet.set_paper(9)  # A4 paper

# Set margins (in inches)
worksheet.set_margins(left=0.5, right=0.5, top=1.0, bottom=1.0)

# Center content on page
worksheet.center_horizontally()
worksheet.center_vertically()

workbook.close()

Headers and Footers

# Simple header and footer
worksheet.set_header('&CWorksheet Title')
worksheet.set_footer('&LPage &P of &N&RCreated: &D')

# Advanced header with formatting
header = '&L&G&C&"Arial,Bold"&14Sales Report&R&"Arial,10"&D'
worksheet.set_header(header, {
    'image_left': 'logo.png',
    'margin': 0.3
})

# Footer with multiple sections
footer = '&L&F&C&"Arial,Bold"Confidential&RPage &P'
worksheet.set_footer(footer)

Print Areas and Titles

# Add sample data
for row in range(50):
    for col in range(10):
        worksheet.write(row, col, f'Cell {row+1},{col+1}')

# Set print area (A1:J20)
worksheet.print_area(0, 0, 19, 9)

# Repeat first row on each page
worksheet.repeat_rows(0)

# Repeat first two columns on each page  
worksheet.repeat_columns(0, 1)

Print Scaling and Fitting

# Scale to 75% of normal size
worksheet.set_print_scale(75)

# Or fit to specific number of pages
# worksheet.fit_to_pages(1, 0)  # Fit to 1 page wide, any height

Print Options

# Hide gridlines in printout
worksheet.hide_gridlines(1)

# Print row and column headers
worksheet.print_row_col_headers()

# Print in black and white
worksheet.print_black_and_white()

# Print across then down
worksheet.print_across()

Page Breaks

# Set horizontal page breaks after rows 10 and 25
worksheet.set_h_pagebreaks([10, 25])

# Set vertical page breaks after columns 5 and 8
worksheet.set_v_pagebreaks([5, 8])

View Modes

# Set to page layout view
worksheet.set_page_view(1)

# Or set to page break preview
worksheet.set_pagebreak_view()

Complete Page Setup Example

# Comprehensive page setup
worksheet.set_landscape()
worksheet.set_paper(9)  # A4
worksheet.set_margins(0.5, 0.5, 0.75, 0.75)
worksheet.center_horizontally()

# Header with logo and title
worksheet.set_header('&L&G&C&"Arial,Bold"&16Monthly Report&R&D', {
    'image_left': 'company_logo.png',
    'margin': 0.5
})

# Footer with filename and page numbers
worksheet.set_footer('&L&F&R&"Arial,10"Page &P of &N')

# Print setup
worksheet.repeat_rows(0, 2)  # Repeat first 3 rows
worksheet.print_area(0, 0, 99, 7)  # Print A1:H100
worksheet.hide_gridlines()
worksheet.fit_to_pages(1, 0)  # Fit to 1 page wide

# Page breaks
worksheet.set_h_pagebreaks([25, 50, 75])  # Break every 25 rows

Header/Footer Formatting Codes Reference

# Common formatting codes for headers/footers:
examples = {
    '&L': 'Left align text',
    '&C': 'Center align text', 
    '&R': 'Right align text',
    '&P': 'Page number',
    '&N': 'Total number of pages',
    '&D': 'Current date',
    '&T': 'Current time',
    '&F': 'File name',
    '&Z': 'Worksheet name',
    '&A': 'Workbook name',
    '&"Arial,12"': 'Set font to Arial, size 12',
    '&"Arial,Bold"': 'Set font to Arial, bold',
    '&G': 'Insert image (use with image options)'
}

# Example usage
complex_header = '&L&"Times New Roman,14"&F&C&"Arial,Bold"&18TITLE&R&"Courier,10"&D &T'
worksheet.set_header(complex_header)

Install with Tessl CLI

npx tessl i tessl/pypi-xlsxwriter

docs

advanced-features.md

charts.md

data-writing.md

formatting.md

index.md

page-setup.md

utilities.md

workbook.md

tile.json