Is there a way to find the page break locations in an xlsxwriter worksheet?

Viewed 43

In my code I generate a worksheet that uses fields with text wrapping, so I don't know exactly how many lines will be on a page when xlsxwriter creates the worksheet. Due to limitations in the app into which I need to import the xlsx worksheet, I need to take my original worksheet and split it so that each page becomes a worksheet in a new workbook. Can I somehow access the location of page breaks after running fit_to_pages(), or alternately, is there a way to know exactly how many rows will be used when you run text wrapping on a field?

1 Answers

Can I somehow access the location of page breaks after running fit_to_pages()

No. That isn't stored in the file format. Excel calculates that at runtime when it loads the file.

Excel also adjusts the height of cells containing wrapped text automatically at runtime. You could prevent this by specifying an explicit row height for the rows that contains the wrapped text so that their height isn't adjusted automatically. However, setting the height explicitly for wrapped text requires some sort of estimation, which takes us to the second part of your question.

or alternately, is there a way to know exactly how many rows will be used when you run text wrapping on a field?

The only way to do this with 100% accuracy would be to use some Windows API functions with bounding box calculations. I'm not sure if that is (non-theoretically) possible in Python and if it is it probably isn't portable.

So you are going to have to make some sort of compromise and use explicit row heights that will allow you to calculate the height of the printed page and thus the page break location but not have the nice automatically height adjusted cells for wrapped text.

Related