After months of research (that wasn't an urgent problem) I found a solution using xlsxwriter Have a look at page setup. It is not as good as it should be however it offers a way to handle page breaks. The code below changes default page breaks of the worksheet:
import xlsxwriter
import random
wb = xlsxwriter.Workbook('page_breaks.xlsx')
ws = wb.add_worksheet('Sample page')
headers = ['header1', 'header2']
# set headers
for col, header in enumerate(headers):
ws.write(0+1, col+1, 'header'+str(col))
# let it be 300 rows of data
for row in xrange(2,302):
ws.write(row, 1, row)
# fill table with some random data
for _ in xrange(3000):
row = random.randrange(2, 302)
col = random.randrange(2, 16)
ws.write(row, col, 'X')
# print_area(), set_paper() and fit_to_pages() do the trick
ws.print_area(1, 1, 301, 15)
ws.set_paper(9) # set A4 as page format
pages_horz = 1
pages_vert = 4
ws.fit_to_pages(pages_horz, pages_vert)
wb.close()
If you want to have certain number of rows on the page you should take care of rows height and choose parges_vert according to it. So you do with columns.
P.S. I've changed xlwt for xlsxwriter because I need charts and some other functionality that xlwt doesn't provide in addition XlsxWriter has excellent documentation. But that doesn't mean xlwt is bad. I guess xlwt has something similar to print_area(), set_paper() and fit_to_pages()
P.P.S. if my answer gets upvotes or there doesn't appear something better I'll mark this with a green tick