from hwpapi import App
with App() as app:
app.open("report.hwp")
action = app.actions.HeaderFooter
pset = action.pset
pset.CtrlType = 0 # 0 = ๋จธ๋ฆฌ๋ง, 1 = ๊ผฌ๋ฆฌ๋ง
pset.Type = 0 # 0 = ์์ชฝ, 1 = ์ง์์ชฝ, 2 = ํ์์ชฝ
action.run(pset)
# ์ก์
์คํ์ผ๋ก ๋จธ๋ฆฌ๋ง ์์ญ์ ์ปค์๊ฐ ๋ค์ด์ด โ ๊ทธ๋ฅ ์
๋ ฅ
app.doc.insert_text("2026 Q1 โ Internal Report")
# ๋ณธ๋ฌธ์ผ๋ก ๋ณต๊ท
app.api.Run("CloseEx")
app.save()Headers and footers
Insert page headers, footers, and page numbers programmatically
๋จธ๋ฆฌ๋ง(header) ๊ณผ ๊ผฌ๋ฆฌ๋ง(footer) ์ HWP ์ ์ปจํธ๋กค(HeaderFooter) ์ด๋ผ, ์ผ๋ฐ ๋ฌธ๋จ ํ
์คํธ๊ฐ ์๋ ์ก์
์ ํตํด ๋ง๋ค์ด์ง๋๋ค. ์ด ๋ ์ํผ๋ ๊ฐ์ฅ ์์ฃผ ์ฐ๋ ๋จธ๋ฆฌ๋ง/๊ผฌ๋ฆฌ๋ง ์๋๋ฆฌ์ค 4๊ฐ์ง๋ฅผ ๋ฌถ์ด ๋ณด์ฌ์ค๋๋ค.
1. ๋ณธ๋ฌธ ์๋จ์ ๋จธ๋ฆฌ๋ง ํ ์คํธ ํ ์ค
2. ํ์ด์ง ๋ฒํธ๊ฐ ๋ค์ด๊ฐ ๊ผฌ๋ฆฌ๋ง
from hwpapi import App
with App() as app:
app.open("report.hwp")
# ๊ผฌ๋ฆฌ๋ง ์ง์
action = app.actions.HeaderFooter
pset = action.pset
pset.CtrlType = 1 # ๊ผฌ๋ฆฌ๋ง
pset.Type = 0 # ์์ชฝ
action.run(pset)
# ๊ฐ์ด๋ฐ ์ ๋ ฌ ํ "n / total" ํ์ ํ์ด์ง ๋ฒํธ
app.api.Run("ParagraphShapeAlignCenter")
app.api.Run("AutoChangeMode") # ํ์ด์ง ์๋ ๋ณ๊ฒฝ ํ์ฑ
app.api.Run("InsertPageNum") # ํ์ฌ ํ์ด์ง ๋ฒํธ
app.doc.insert_text(" / ")
app.api.Run("InsertPageCount") # ์ด ํ์ด์ง ์
app.api.Run("CloseEx")
app.save()3. ์ง์/ํ์์ชฝ ๋ค๋ฅธ ๋จธ๋ฆฌ๋ง
์๋ฉด ์ธ์์์ ํํ ํจํด โ ์ง์์ชฝ์ ์ฑ ์ ๋ชฉ, ํ์์ชฝ์ ์ฅ ์ ๋ชฉ.
from hwpapi import App
def insert_header(app, text: str, *, type_: int) -> None:
"""type_: 1=์ง์์ชฝ, 2=ํ์์ชฝ"""
action = app.actions.HeaderFooter
pset = action.pset
pset.CtrlType = 0
pset.Type = type_
action.run(pset)
app.api.Run(
"ParagraphShapeAlignLeft" if type_ == 2 else "ParagraphShapeAlignRight"
)
app.doc.insert_text(text)
app.api.Run("CloseEx")
with App() as app:
app.open("book.hwp")
insert_header(app, "ํ๊ธ ์๋ํ ๊ฐ์ด๋", type_=1) # ์ง์
insert_header(app, "Chapter 3 โ Collections", type_=2) # ํ์
app.save()4. ๋ชจ๋ ๋จธ๋ฆฌ๋ง/๊ผฌ๋ฆฌ๋ง ์ง์ฐ๊ธฐ
from hwpapi import App
with App() as app:
app.open("report.hwp")
# ํ ๋ฒ ํธ์ถ์ ๋ชจ๋ ๋จธ๋ฆฌ๋ง/๊ผฌ๋ฆฌ๋ง ์ปจํธ๋กค ์ ๊ฑฐ
app.api.Run("HeaderFooterDelete")
app.save()