Reputation: 21
I am very new to Python and am experimenting with creating and printing excel spreadsheets. Just for the record, I have many years of experience in C and C++ programming. I am using Python 3.5.1 and OpenPyxl on Windows 7 and working with xlsx files.The code snippet I am using to repeat top 2 rows as repeating page headers is reproduced below:
import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.properties import WorksheetProperties
...
sheet.print_options.gridLines = True
sheet.print_title_rows = '1:2' #**problem code not working as intended**
sheet.page_setup.orientation = sheet.ORIENTATION_LANDSCAPE
...
All other settings above are working as expected except for the assignment statement to print_title_rows in the active worksheet. I will be immensely grateful to gurus on this board if they can point me in the right direction. Appreciate your time.
Upvotes: 2
Views: 2250
Reputation: 697
The following works for me:
ws.print_title_cols = 'A:B' # the first two cols
ws.print_title_rows = '1:2' # the first two rows
Also, sheet.add_print_title(2)
is deprecated since 2.4.0-a1 (2016-04-11).
Viewing the output file: at least in LibreOffice 6.0 the rows/columns are not repeated. Works fine in MS Excel 2013 though. Bug #973
source: openpyxl documentation
Upvotes: 5
Reputation: 1277
Replace
sheet.print_title_rows = '1:2'
with
sheet.add_print_title(2)
You can also repeat columns using 'cols' as the second argument of the method.
See the official documentation add_print_title(n, rows_or_cols='rows')
Upvotes: 0