Skip to content

How to set a table row to repeated as header? #322

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

Open
ssuzen opened this issue Oct 4, 2016 · 18 comments
Open

How to set a table row to repeated as header? #322

ssuzen opened this issue Oct 4, 2016 · 18 comments

Comments

@ssuzen
Copy link

ssuzen commented Oct 4, 2016

I am trying to enable "Repeat Header Rows" for a table row

@raphaelvalentin
Copy link

raphaelvalentin commented Dec 6, 2016

@ssuzen
Hi, sorry for the delay. This is a snippet you could use:

def set_repeat_table_header(row):
    """ set repeat table row on every new page
    """
    tr = row._tr
    trPr = tr.get_or_add_trPr()
    tblHeader = OxmlElement('w:tblHeader')
    tblHeader.set(qn('w:val'), "true")
    trPr.append(tblHeader)
    return row```

@ssuzen
Copy link
Author

ssuzen commented Dec 6, 2016

Thank you. This works just fine. I appreciate it.

@pierreluc-plt
Copy link

Hi, could you provide a small example of how you would implement that function? When and where would you call it when you create the table?

Thanks!

@lingxiaoyang
Copy link

@pierreluc-plt If you have a table, you can call set_repeat_table_header(table.rows[0]) for the setup.

@answerquest
Copy link

Just cross-posting the required import statements for this from here:

import docx
from docx.oxml import OxmlElement
from docx.oxml.ns import qn

@DanielKorsa
Copy link

Hi,
could you clarify one thing. Would you use this function after a Table created? Or after it is filled out?
Would really appreciate an example cuz so far it didnt work out. Thanks in advance!

@pacovl
Copy link

pacovl commented Mar 25, 2021

A simple example that creates a document with just a table and its header repeated in both pages:

from docx import Document
from docx.oxml.shared import OxmlElement,qn
 
def set_repeat_table_header(row):
    """ set repeat table row on every new page
    """
    tr = row._tr
    trPr = tr.get_or_add_trPr()
    tblHeader = OxmlElement('w:tblHeader')
    tblHeader.set(qn('w:val'), "true")
    trPr.append(tblHeader)
    return row
 
document = Document()
 
table = document.add_table(70,2)
table.autofit=False
table.style = 'Table Grid'
 
cell_00 = table.cell(0, 0)
cell_00.text = 'title 1'
 
cell_01 = table.cell(0, 1)
cell_01.text = 'title 2'
 
set_repeat_table_header(table.rows[0])
 
document.save('table.docx')

@D-Iskimzhy
Copy link

Hi All!
How to set 2 or more rows in table to be as header on every new page?
Thank you!

@D-Iskimzhy
Copy link

D-Iskimzhy commented Jul 3, 2022

Hi All! How to set 2 or more rows in table to be as header on every new page? Thank you!

did it as While loop

i=0
while i<=1:
    set_repeat_table_header(table.rows[i])
    i=i+1

@keithbriggs
Copy link

Isn't that just the same as:

set_repeat_table_header(table.rows[0])
set_repeat_table_header(table.rows[1])

?

@D-Iskimzhy
Copy link

D-Iskimzhy commented Jul 3, 2022

Yeah!
I came to that afer posting about loop

@keithbriggs
Copy link

In any case, in python we write i+=1, not i=i+1.

@D-Iskimzhy
Copy link

In any case, in python we write i+=1, not i=i+1.

Oh!
Thanks! Always forget about that)
I'm new to python, its like hobby for me.

@greglu1976
Copy link

I set double line bottom on header row, but on the next page a single line header bottom remains...

@akhilrajmr-spericorn
Copy link

Thank you. This works

@obentley2
Copy link

Hi, I am having trouble with the code above and it isn't working for me. My table is inside another table to adhere to a template to give it a name and caption. Would this be the cause of the problem?
Thanks

@ramgavate68
Copy link

hye can any one suggest me why it not working in angular project?
const table4 = new Table({
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
margins: {
top: 100,
bottom: 100,
left: 100,
right: 100,
},
rows: [
new TableRow({
tableHeader: true,
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: "S.No", bold: true, size: 18 })],
alignment: AlignmentType.CENTER
})],
width: { size: 10, type: WidthType.PERCENTAGE }
}),

@ramgavate68
Copy link

hye buddies can any one help me in ngular TS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests