Set up printing and paginate a sheet
Use this page to make a sheet print the way Excel would print it, and to find out how many pages it takes and which cells land on each page, without a printer or Excel. Presentation, geometry, and pagination covers the print model, and the calls are under Print settings and pagination in the WASM API and the Python API.
Set the print area and print titles
setPrintArea stores the sheet's print area (Excel's Print_Area name); it takes one A1 range, a list of ranges, or null to remove it. setPrintTitles repeats rows, columns or both on every page: a row span is '1' or '1:2', a column span 'A' or 'A:B', and null repeats nothing on that axis.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
const rows = Array.from({ length: 120 }, (_, i) => [`Item ${i + 1}`, i + 1, (i + 1) * 1.5])
ws.setValues('A1', [['Item', 'Qty', 'Price'], ...rows])
ws.setPrintArea('A1:C121')
ws.setPrintTitles({ rows: '1', columns: null })
console.log(JSON.stringify(ws.getPrintArea())) // [{"firstRow":0,"firstCol":0,"lastRow":120,"lastCol":2}]
console.log(JSON.stringify(ws.getPrintTitles())) // {"rows":{"first":0,"last":0},"columns":null}
} finally {
wb.dispose()
}from formulon import PrintTitlesInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
rows = [[f"Item {i}", i, i * 1.5] for i in range(1, 121)]
ws.set_values("A1", [["Item", "Qty", "Price"], *rows])
ws.set_print_area("A1:C121")
ws.set_print_titles(PrintTitlesInput(rows="1", columns=None))
print(ws.get_print_area()) # [CellRect(first_row=0, first_col=0, last_row=120, last_col=2)]
print(ws.get_print_titles()) # PrintTitles(rows=Span(first=0, last=0), columns=None)Without a print area, pagination covers the sheet's used range.
Choose orientation, paper and scaling
updatePageSetup(patch) sets the orientation, the paper size (an OOXML code; PaperSize names the common ones), and the scaling. Scaling is either a fixed scalePct from 10 to 400, or fit-to-page: fitToPage: true with fitToWidth and fitToHeight pages, where 0 leaves that direction unconstrained. A key left out keeps its value.
import createFormulon, { PageOrientation, PaperSize } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
const rows = Array.from({ length: 120 }, (_, i) => [`Item ${i + 1}`, i + 1, (i + 1) * 1.5])
ws.setValues('A1', [['Item', 'Qty', 'Price'], ...rows])
ws.updatePageSetup({ orientation: PageOrientation.Landscape, paperSize: PaperSize.A4 })
console.log(ws.getPagination().pageCount) // 4
ws.updatePageSetup({ fitToPage: true, fitToWidth: 1, fitToHeight: 1 })
console.log(ws.getPagination().pageCount) // 1
ws.updatePageSetup({ fitToPage: false, scalePct: 50 })
const pagination = ws.getPagination()
console.log(pagination.pageCount, pagination.scalePct) // 2 50
} finally {
wb.dispose()
}from formulon import PageOrientation, PaperSize, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
rows = [[f"Item {i}", i, i * 1.5] for i in range(1, 121)]
ws.set_values("A1", [["Item", "Qty", "Price"], *rows])
ws.update_page_setup(orientation=PageOrientation.LANDSCAPE, paper_size=PaperSize.A4)
print(ws.get_pagination().page_count) # 4
ws.update_page_setup(fit_to_page=True, fit_to_width=1, fit_to_height=1)
print(ws.get_pagination().page_count) # 1
ws.update_page_setup(fit_to_page=False, scale_pct=50)
pagination = ws.get_pagination()
print(pagination.page_count, pagination.scale_pct) # 2 50.0getPageSetup() reads the settings back. Each value has a *Stated flag that tells a value the file declares from Excel's default.
Set margins, header and footer
updatePageMargins takes margins in inches. updateHeaderFooter takes each header and footer section as Excel's code text: &L, &C and &R start the left, centre and right parts, &P is the page number and &N the page count. updatePrintOptions turns on gridlines and row and column headings and centres the print on the page.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValue('A1', 'Report')
ws.updatePageMargins({ leftIn: 0.5, rightIn: 0.5, topIn: 0.75, bottomIn: 0.75 })
ws.updateHeaderFooter({ oddHeader: '&CSales report', oddFooter: '&RPage &P of &N' })
ws.updatePrintOptions({ gridLines: true, horizontalCentered: true })
const margins = ws.getPageMargins()
console.log(margins.leftIn, margins.topIn, margins.headerIn) // 0.5 0.75 0.3
console.log(ws.getHeaderFooter().oddFooter) // &RPage &P of &N
console.log(ws.getPrintOptions().gridLines) // true
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1"] = "Report"
ws.update_page_margins(left_in=0.5, right_in=0.5, top_in=0.75, bottom_in=0.75)
ws.update_header_footer(odd_header="&CSales report", odd_footer="&RPage &P of &N")
ws.update_print_options(grid_lines=True, horizontal_centered=True)
margins = ws.get_page_margins()
print(margins.left_in, margins.top_in, margins.header_in) # 0.5 0.75 0.3
print(ws.get_header_footer().odd_footer) # &RPage &P of &N
print(ws.get_print_options().grid_lines) # TruedifferentFirst and differentOddEven switch on the separate first-page and even-page sections.
Insert page breaks
setRowBreak(row, input) puts a manual break before the 0-based row, and setColumnBreak(col, input) before a column. An empty input covers the whole sheet; null removes the break. listRowBreaks() lists the manual breaks the sheet stores, and clearBreaks() removes them all. Pagination then starts a page at each manual break and adds its own automatic breaks where a page fills up.
import createFormulon, { PageOrientation } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
const rows = Array.from({ length: 120 }, (_, i) => [`Item ${i + 1}`, i + 1, (i + 1) * 1.5])
ws.setValues('A1', [['Item', 'Qty', 'Price'], ...rows])
ws.updatePageSetup({ orientation: PageOrientation.Landscape })
ws.setRowBreak(40, {})
console.log(JSON.stringify(ws.listRowBreaks().map((b) => [b.row, b.manual]))) // [[40,true]]
const breaks = ws.getPagination().rowBreaks
console.log(breaks.map((b) => b.row).join()) // 33,40,73,106
} finally {
wb.dispose()
}from formulon import PageBreakInput, PageOrientation, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
rows = [[f"Item {i}", i, i * 1.5] for i in range(1, 121)]
ws.set_values("A1", [["Item", "Qty", "Price"], *rows])
ws.update_page_setup(orientation=PageOrientation.LANDSCAPE)
ws.set_row_break(40, PageBreakInput())
print([(b.row, b.manual) for b in ws.list_row_breaks()]) # [(40, True)]
breaks = ws.get_pagination().row_breaks
print(",".join(str(b.row) for b in breaks)) # 33,40,73,106Paginate the sheet
getPagination() lays the sheet out on paper the way Excel's print preview does. The result has pageCount, the breaks in effect, the paper and margins, the effective scalePct, the repeated titles, and pages: one entry per physical page in print order, each with its cell range and its position on the paper in points. The calculation uses Excel's print column widths, so the page boundaries match Excel's.
The CLI's paginate command prints the same layout for a saved .xlsx or .xlsb file.
import { writeFile } from 'node:fs/promises'
import createFormulon, { PageOrientation } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
const rows = Array.from({ length: 120 }, (_, i) => [`Item ${i + 1}`, i + 1, (i + 1) * 1.5])
ws.setValues('A1', [['Item', 'Qty', 'Price'], ...rows])
ws.setPrintArea('A1:C121')
ws.setPrintTitles({ rows: '1', columns: null })
ws.updatePageSetup({ orientation: PageOrientation.Landscape })
ws.setRowBreak(40, {})
const pagination = ws.getPagination()
console.log(pagination.pageCount) // 5
const ranges = pagination.pages.map((p) => `${p.range.firstRow + 1}-${p.range.lastRow + 1}`)
console.log(ranges.join(' ')) // 1-28 29-40 41-68 69-96 97-121
console.log(pagination.paper.landscape, Math.round(pagination.paper.widthPt)) // true 842
await writeFile('report.xlsx', wb.save())
} finally {
wb.dispose()
}from formulon import PageBreakInput, PageOrientation, PrintTitlesInput, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
rows = [[f"Item {i}", i, i * 1.5] for i in range(1, 121)]
ws.set_values("A1", [["Item", "Qty", "Price"], *rows])
ws.set_print_area("A1:C121")
ws.set_print_titles(PrintTitlesInput(rows="1", columns=None))
ws.update_page_setup(orientation=PageOrientation.LANDSCAPE)
ws.set_row_break(40, PageBreakInput())
pagination = ws.get_pagination()
print(pagination.page_count) # 5
ranges = [f"{p.range.first_row + 1}-{p.range.last_row + 1}" for p in pagination.pages]
print(" ".join(ranges)) # 1-28 29-40 41-68 69-96 97-121
print(pagination.paper.landscape, round(pagination.paper.width_pt)) # True 842
wb.save("report.xlsx")formulon paginate report.xlsx
# name=Sheet1
# pageCount=5
# printArea=A1:C121
# rowBreaks=28,40,68,96
# columnBreaks=
# pages=A1:C28,A29:C40,A41:C68,A69:C96,A97:C121
formulon paginate --json report.xlsxA page's range holds its own cells only; the repeated title rows print above it on every page after the first. A host that draws a print preview places each page's cells at originXPt / originYPt and scales them by scalePct.