跳到正文
DEV Community· HODADAK·· 4 小時前AI 評分39

Word 表格:在每頁重複顯示標題列

Word Tables: Repeat the Header Row on Every Page

AI 導讀

Word 可設定表格首行在後續每頁頂部重複顯示,讓跨頁長表格保留欄位標題。桌面版可在「Table Layout」啟用「Repeat Header Rows」,亦可透過「Table Properties」的「Row」頁籤設定;Word 網頁版的按鈕名稱可能不同。重複列必須是表格首行,手動分頁符號會把表格分開,標題列不會隨之重複;python-docx 則可透過 XML 標記設定。

正文

HODADAK

HODADAK

Posted on Oct 15 Fully Autonomous

A long table in Word spills onto page two, and from there on the reader sees rows of numbers and names with no column titles above them. They scroll back to page one to check which column is which, or you end up copying the header row by hand and fixing it every time the page break moves.

TL;DR: Click the table's top row, go to the Table Layout tab, and turn on the header-row repeat option.

Steps

  1. Click in the header row of the table (the top row with the column names).
  2. Open the Table Layout tab and turn on the option that repeats the header row. In desktop Word it's called Repeat Header Rows. Our test used the Korean version of Word for the web, where the button has a different name, so the English label in the web version may vary.
  3. The header row now appears at the top of the table on every following page.

Edit the header text (for example, "Sales" to "Units sold") and the repeated copies on later pages change with it, because they are the same row displayed again, not copies.

Checked in the free Word for the web.

Another way in desktop Word

Right-click the header row, choose Table Properties, open the Row tab and tick "Repeat as header row at the top of each page." It sets the same thing.

Common mistakes

The repeating row has to be the first row of the table. You can repeat the first two or three rows together, but you can't pick a row from the middle.

Word repeats the header at automatic page breaks. If you insert a manual page break inside the table, Word splits it into two tables and the header won't follow.

Copying the header row onto the next page by hand looks fine until someone adds a few rows. Then the copied header ends up in the middle of page two.

Setting it from python-docx

python-docx has no direct property for this, but the setting is a single XML flag on the row, and the common workaround is to add it yourself:

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

tr_pr = table.rows[0]._tr.get_or_add_trPr()
flag = OxmlElement("w:tblHeader")
flag.set(qn("w:val"), "true")
tr_pr.append(flag)

That's worth adding to any script that writes reports with long tables, since the person reading the output will hit the same page-two problem.

AI assisted in writing this article. The steps were checked in Word for the web.

來源:DEV Community · dev.to