-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: Row.allow_break_across_pages #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Fixed the issue following the information in this thread: #55 (comment) Thanks for the tool @scanny it's extremely helpful =) |
Reopening as feature request for |
@scanny here's the code that I ended up using to fix the issue:
I only had a single table in my document so I just applied it to every cell I believe. It's some edited code I found in another one of your comments I believe, but figured it doesn't hurt to post it. Thanks again, python-docx has made a huge difference in my job and is the reason I learned Python. |
Super, thanks Joe :) |
For those people that are looking to do the opposite and allow the row to go over multiple pages.
|
looks like in the current format you have to have trPr tag (table row properties) for such tr. In the trPr you can specify property OxmlElement("w:cantSplit"). Rather life-hack (since it's internal api), to set "no-break" for a row:
|
it fixed by trPr.append(cantSplit) |
@1krishnasharma the solutions here should work for you. A slightly more robust implementation would be: from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.table import _Row
def make_row_cant_split(row: _Row) -> None:
tr = row._tr
# -- if the element is already present, make sure it's turned on --
cantSplits = tr.xpath("./w:trPr/w:cantSplit")
if cantSplits:
cantSplit = cantSplits[0]
cantSplit.set(qn('w:val'), 'true')
return
# -- otherwise add it in bool-true state --
trPr = tr.get_or_add_trPr()
cantSplit = OxmlElement("w:cantSplit")
cantSplit.set(qn('w:val'), 'true')
trPr.insert_element_before(
cantSplit,
(
"w:trHeight",
"w:tblHeader",
"w:tblCellSpacing",
"w:jc",
"w:hidden",
"w:ins",
"w:del",
"w:trPrChange",
),
) |
@scanny Thanks for the robust implementation 👍 Can I ask why it's better to use Thanks again |
@ShoulddaBeenaWhaleBiologist In general, child elements in the OpenXML schema are specified as a sequence, meaning they have a specified order. Sometimes this order matters, so placing a new element in the right position is something we always do in the library. That's what
|
I'll try to keep it sweet and simple... There is a variable, paragraph.paragraph_format.keep_together, when it's true it will keep the paragraph on a single page instead of splitting it if it's at the end of the page.
I tried using this in a table scenario and it didn't work, although Word has the capability. After comparing the document.xml between a document where a row can split pages and one that cannot, I found that it acts as a table row property instead of a paragraph property (cantSplit vs keepLines in XML).
I can't seem to find any info regarding this within python-docx so I may make my own docx function for it, but I've got very little XML knowledge so I'd like to avoid that.
References for @scanny if he sees this:
That's the XML with the row not splitting pages and this is without:
Obvious difference is the fact there's no table row property section and also the initial table row definition is different. If I end up making my own XML edits for this I'll make sure to post them.
The text was updated successfully, but these errors were encountered: