Changing default page break in xls file through Python xlwt module

I am trying to edit page breaks using the Python xlwt module. (Yes, I found a very similar question xlwt - How to add page breaks to an Excel file? But this did not completely fix the problem)

Here is some code that doesn't work as I expect:

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('Report')
ws.write(0,0, '0 here')
ws.write(43,12, '12 here')
ws.horz_page_breaks = [(44,0,12),]
wb.save('sample.xls')

It creates a row page break, but the columns appear to have a default page break (The page starts from column "K", which is the 10th).

Can someone explain what I'm doing wrong or how to make a strict page break?

PS I was advised to look at the openpyxl library, but did not find any documentation or an example of how to set a page break.

UPD: , . Excel, ( http://office.microsoft.com/en-au/excel-help/insert-move-or-delete-page-breaks-in-a-worksheet-HP010021539.aspx), . workheet.vert_page_break ( ) . .

xlwt, .

+4
1

( ) , xlsxwriter . , , . :

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()

, parges_vert . , .

P.S. xlwt xlsxwriter, , xlwt . XlsxWriter . , xlwt . , xlwt print_area(), set_paper() fit_to_pages()

P.P.S. upvotes - ,

+2

Source: https://habr.com/ru/post/1537637/


All Articles