or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mdcharts.mddata-writing.mdformatting.mdindex.mdpage-setup.mdutilities.mdworkbook.md

page-setup.mddocs/

0

# Page Setup and Printing

1

2

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.

3

4

## Capabilities

5

6

### Page Orientation and View

7

8

Configure page orientation and view modes.

9

10

```python { .api }

11

def set_landscape(self):

12

"""Set page orientation to landscape."""

13

14

def set_portrait(self):

15

"""Set page orientation to portrait (default)."""

16

17

def set_page_view(self, view=1):

18

"""

19

Set the page view mode.

20

21

Args:

22

view (int): View mode:

23

- 0: Normal view

24

- 1: Page Layout view

25

- 2: Page Break Preview

26

"""

27

28

def set_pagebreak_view(self):

29

"""Set the worksheet to Page Break Preview mode."""

30

```

31

32

### Paper Size and Scaling

33

34

Configure paper size and print scaling options.

35

36

```python { .api }

37

def set_paper(self, paper_size):

38

"""

39

Set the paper size for printing.

40

41

Args:

42

paper_size (int): Paper size code:

43

- 1: Letter (8.5 x 11 in)

44

- 5: Legal (8.5 x 14 in)

45

- 9: A4 (210 x 297 mm)

46

- 11: A5 (148 x 210 mm)

47

- 20: Envelope #10 (4.125 x 9.5 in)

48

- And many other standard paper sizes

49

"""

50

51

def set_print_scale(self, scale):

52

"""

53

Set the print scale percentage.

54

55

Args:

56

scale (int): Scale percentage (10-400)

57

58

Note: Cannot be used with fit_to_pages()

59

"""

60

61

def fit_to_pages(self, width, height):

62

"""

63

Fit the worksheet to a specific number of pages.

64

65

Args:

66

width (int): Number of pages wide (0 for no limit)

67

height (int): Number of pages tall (0 for no limit)

68

69

Note: Cannot be used with set_print_scale()

70

"""

71

```

72

73

### Page Margins

74

75

Configure page margins for printing.

76

77

```python { .api }

78

def set_margins(self, left=0.7, right=0.7, top=0.75, bottom=0.75):

79

"""

80

Set page margins in inches.

81

82

Args:

83

left (float): Left margin in inches

84

right (float): Right margin in inches

85

top (float): Top margin in inches

86

bottom (float): Bottom margin in inches

87

"""

88

```

89

90

### Page Centering

91

92

Center content on the printed page.

93

94

```python { .api }

95

def center_horizontally(self):

96

"""Center the worksheet data horizontally on the page."""

97

98

def center_vertically(self):

99

"""Center the worksheet data vertically on the page."""

100

```

101

102

### Headers and Footers

103

104

Configure page headers and footers with dynamic content.

105

106

```python { .api }

107

def set_header(self, header="", options=None, margin=None):

108

"""

109

Set the page header.

110

111

Args:

112

header (str): Header text with optional formatting codes:

113

- &L: Left section

114

- &C: Center section

115

- &R: Right section

116

- &P: Page number

117

- &N: Total pages

118

- &D: Current date

119

- &T: Current time

120

- &F: File name

121

- &Z: Worksheet name

122

- &A: Workbook name

123

- &"font,size": Font formatting

124

- &G: Insert image

125

options (dict, optional): Header options:

126

- margin (float): Header margin in inches

127

- image_left (str): Left section image file

128

- image_center (str): Center section image file

129

- image_right (str): Right section image file

130

- image_data_left (bytes): Left section image data

131

- image_data_center (bytes): Center section image data

132

- image_data_right (bytes): Right section image data

133

margin (float, optional): Header margin (deprecated, use options)

134

"""

135

136

def set_footer(self, footer="", options=None, margin=None):

137

"""

138

Set the page footer.

139

140

Args:

141

footer (str): Footer text with same formatting codes as header

142

options (dict, optional): Same options as set_header

143

margin (float, optional): Footer margin (deprecated, use options)

144

"""

145

```

146

147

### Print Areas and Titles

148

149

Define what gets printed and repeated on each page.

150

151

```python { .api }

152

def print_area(self, first_row, first_col, last_row, last_col):

153

"""

154

Set the print area for the worksheet.

155

156

Args:

157

first_row (int): First row of print area (0-indexed)

158

first_col (int): First column of print area (0-indexed)

159

last_row (int): Last row of print area (0-indexed)

160

last_col (int): Last column of print area (0-indexed)

161

"""

162

163

def repeat_rows(self, first_row, last_row=None):

164

"""

165

Set rows to repeat at the top of each printed page.

166

167

Args:

168

first_row (int): First row to repeat (0-indexed)

169

last_row (int, optional): Last row to repeat (defaults to first_row)

170

"""

171

172

def repeat_columns(self, first_col, last_col=None):

173

"""

174

Set columns to repeat at the left of each printed page.

175

176

Args:

177

first_col (int): First column to repeat (0-indexed)

178

last_col (int, optional): Last column to repeat (defaults to first_col)

179

"""

180

```

181

182

### Print Options

183

184

Configure various print display options.

185

186

```python { .api }

187

def hide_gridlines(self, option=1):

188

"""

189

Hide gridlines in the printed output.

190

191

Args:

192

option (int): Gridline hiding option:

193

- 0: Show gridlines (default Excel behavior)

194

- 1: Hide printed gridlines

195

- 2: Hide screen and printed gridlines

196

"""

197

198

def print_row_col_headers(self):

199

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

200

201

def hide_row_col_headers(self):

202

"""Hide row and column headers in printed output."""

203

204

def print_black_and_white(self):

205

"""Print in black and white."""

206

207

def print_across(self):

208

"""

209

Set page order to print across then down.

210

Default is down then across.

211

"""

212

```

213

214

### Page Breaks

215

216

Manually control where page breaks occur.

217

218

```python { .api }

219

def set_h_pagebreaks(self, breaks):

220

"""

221

Set horizontal page breaks.

222

223

Args:

224

breaks (list): List of row numbers (0-indexed) where breaks occur

225

"""

226

227

def set_v_pagebreaks(self, breaks):

228

"""

229

Set vertical page breaks.

230

231

Args:

232

breaks (list): List of column numbers (0-indexed) where breaks occur

233

"""

234

```

235

236

### Print Start Page

237

238

Set the starting page number for printing.

239

240

```python { .api }

241

def set_start_page(self, start_page):

242

"""

243

Set the starting page number.

244

245

Args:

246

start_page (int): Starting page number (default 1)

247

"""

248

```

249

250

## Usage Examples

251

252

### Basic Page Setup

253

254

```python

255

import xlsxwriter

256

257

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

258

worksheet = workbook.add_worksheet()

259

260

# Set orientation and paper size

261

worksheet.set_landscape()

262

worksheet.set_paper(9) # A4 paper

263

264

# Set margins (in inches)

265

worksheet.set_margins(left=0.5, right=0.5, top=1.0, bottom=1.0)

266

267

# Center content on page

268

worksheet.center_horizontally()

269

worksheet.center_vertically()

270

271

workbook.close()

272

```

273

274

### Headers and Footers

275

276

```python

277

# Simple header and footer

278

worksheet.set_header('&CWorksheet Title')

279

worksheet.set_footer('&LPage &P of &N&RCreated: &D')

280

281

# Advanced header with formatting

282

header = '&L&G&C&"Arial,Bold"&14Sales Report&R&"Arial,10"&D'

283

worksheet.set_header(header, {

284

'image_left': 'logo.png',

285

'margin': 0.3

286

})

287

288

# Footer with multiple sections

289

footer = '&L&F&C&"Arial,Bold"Confidential&RPage &P'

290

worksheet.set_footer(footer)

291

```

292

293

### Print Areas and Titles

294

295

```python

296

# Add sample data

297

for row in range(50):

298

for col in range(10):

299

worksheet.write(row, col, f'Cell {row+1},{col+1}')

300

301

# Set print area (A1:J20)

302

worksheet.print_area(0, 0, 19, 9)

303

304

# Repeat first row on each page

305

worksheet.repeat_rows(0)

306

307

# Repeat first two columns on each page

308

worksheet.repeat_columns(0, 1)

309

```

310

311

### Print Scaling and Fitting

312

313

```python

314

# Scale to 75% of normal size

315

worksheet.set_print_scale(75)

316

317

# Or fit to specific number of pages

318

# worksheet.fit_to_pages(1, 0) # Fit to 1 page wide, any height

319

```

320

321

### Print Options

322

323

```python

324

# Hide gridlines in printout

325

worksheet.hide_gridlines(1)

326

327

# Print row and column headers

328

worksheet.print_row_col_headers()

329

330

# Print in black and white

331

worksheet.print_black_and_white()

332

333

# Print across then down

334

worksheet.print_across()

335

```

336

337

### Page Breaks

338

339

```python

340

# Set horizontal page breaks after rows 10 and 25

341

worksheet.set_h_pagebreaks([10, 25])

342

343

# Set vertical page breaks after columns 5 and 8

344

worksheet.set_v_pagebreaks([5, 8])

345

```

346

347

### View Modes

348

349

```python

350

# Set to page layout view

351

worksheet.set_page_view(1)

352

353

# Or set to page break preview

354

worksheet.set_pagebreak_view()

355

```

356

357

### Complete Page Setup Example

358

359

```python

360

# Comprehensive page setup

361

worksheet.set_landscape()

362

worksheet.set_paper(9) # A4

363

worksheet.set_margins(0.5, 0.5, 0.75, 0.75)

364

worksheet.center_horizontally()

365

366

# Header with logo and title

367

worksheet.set_header('&L&G&C&"Arial,Bold"&16Monthly Report&R&D', {

368

'image_left': 'company_logo.png',

369

'margin': 0.5

370

})

371

372

# Footer with filename and page numbers

373

worksheet.set_footer('&L&F&R&"Arial,10"Page &P of &N')

374

375

# Print setup

376

worksheet.repeat_rows(0, 2) # Repeat first 3 rows

377

worksheet.print_area(0, 0, 99, 7) # Print A1:H100

378

worksheet.hide_gridlines()

379

worksheet.fit_to_pages(1, 0) # Fit to 1 page wide

380

381

# Page breaks

382

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

383

```

384

385

### Header/Footer Formatting Codes Reference

386

387

```python

388

# Common formatting codes for headers/footers:

389

examples = {

390

'&L': 'Left align text',

391

'&C': 'Center align text',

392

'&R': 'Right align text',

393

'&P': 'Page number',

394

'&N': 'Total number of pages',

395

'&D': 'Current date',

396

'&T': 'Current time',

397

'&F': 'File name',

398

'&Z': 'Worksheet name',

399

'&A': 'Workbook name',

400

'&"Arial,12"': 'Set font to Arial, size 12',

401

'&"Arial,Bold"': 'Set font to Arial, bold',

402

'&G': 'Insert image (use with image options)'

403

}

404

405

# Example usage

406

complex_header = '&L&"Times New Roman,14"&F&C&"Arial,Bold"&18TITLE&R&"Courier,10"&D &T'

407

worksheet.set_header(complex_header)

408

```