์ด ๊ฐ์ด๋๋ python-hwpx๋ฅผ ์ฒ์ ์ ํ๋ ๋ถ์ ์ํ ์ด๊ฐ๋จ ํํ ๋ฆฌ์ผ์
๋๋ค. ์ค์น๋ถํฐ ๋ฌธ์๋ฅผ ์ด๊ณ ์์ ํ ๋ค ์ ์ฅํ๋ ๊ณผ์ ๊น์ง ํต์ฌ ํ๋ฆ๋ง ๋น ๋ฅด๊ฒ ์ตํ๋๋ค.
pip install python-hwpxํจํค์ง์๋ ๊ธฐ๋ณธ ํ
ํ๋ฆฟ์ด ํจ๊ป ํฌํจ๋์ด ์์ต๋๋ค. ์ค์ต์ฉ ๋ฌธ์๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด hwpx.templates.blank_document_bytes()๋ฅผ ์ฌ์ฉํด ๋น ๋ฌธ์๋ฅผ ์ฆ์ ์์ฑํ ์ ์์ต๋๋ค. ๋ณด์ ์ค์ธ HWPX ํ์ผ์ด ์๋ค๋ฉด ๊ฒฝ๋ก๋ง ๊ต์ฒดํด ์คํํด๋ ๋ฉ๋๋ค.
Jupyter Notebook์ด๋ IPython์์ ์คํํ๋ฉด XML ๊ตฌ์กฐ๋ฅผ ๊พธ์คํ ํ์ํ๋ฉด์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๊ธฐ ํธ๋ฆฌํฉ๋๋ค.
from io import BytesIO
from hwpx import HwpxDocument
from hwpx.templates import blank_document_bytes
source = BytesIO(blank_document_bytes())
document = HwpxDocument.open(source)
print("์ด ์น์
์:", len(document.sections))
print("์ฒซ ๋ฒ์งธ ๋ฌธ๋จ ํ
์คํธ:", document.paragraphs[0].text)HwpxDocument.open()์ ํ์ผ ๊ฒฝ๋ก, ๋ฐ์ดํธ, ํ์ผ ๊ฐ์ฒด ๋ฑ ๋ค์ํ ์
๋ ฅ์ ๋ฐ์ ๋ฌธ์๋ฅผ ๋ก๋ํฉ๋๋ค. ๋ฐํ๋ document ๊ฐ์ฒด๋ ์น์
, ๋ฌธ๋จ, ํ ๋ฑ ์ฃผ์ ๊ตฌ์ฑ ์์์ ๋ฐ๋ก ์ ๊ทผํ ์ ์๋ ๊ณ ์์ค API๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ปจํ
์คํธ ๋งค๋์ (with)์ ํจ๊ป ์ฌ์ฉํ๋ฉด ๋ธ๋ก ์ข
๋ฃ ์์ (์ ์/์์ธ ๋ชจ๋)์๋ ๋ด๋ถ ์์ ์ ๋ฆฌ๊ฐ ์๋์ผ๋ก ์ํ๋ฉ๋๋ค.
from hwpx import HwpxDocument
with HwpxDocument.open("input/sample.hwpx") as document:
document.add_paragraph("with ๋ธ๋ก ์์์ ์์ ํ๊ฒ ํธ์ง")
document.save("output/sample-updated.hwpx")section = document.sections[0]
paragraph = document.add_paragraph(
"python-hwpx๋ก ์์ฑํ ๋ฌธ๋จ์
๋๋ค.",
section=section,
)
print("์ถ๊ฐ๋ ๋ฌธ๋จ:", paragraph.text)๋ฌธ๋จ์ ํญ์ ํน์ ์น์
์ ์ํฉ๋๋ค. ์น์
์ ์ง์ ํ์ง ์์ผ๋ฉด ๋ง์ง๋ง ์น์
์ ์ฌ์ฉํฉ๋๋ค. ๋ฆฌํด๊ฐ์ผ๋ก ๋ฐ์ paragraph๋ ๋ฐ(run) ์ถ๊ฐ, ๋ฉ๋ชจ ์ต์ปค ์ฐ๊ฒฐ ๋ฑ ํ์ ์กฐ์์ ํ ๋ ๊ณ์ ํ์ฉํ ์ ์์ต๋๋ค.
table = document.add_table(rows=2, cols=2, section=section)
table.set_cell_text(0, 0, "ํญ๋ชฉ")
table.set_cell_text(0, 1, "๊ฐ")
table.set_cell_text(1, 0, "๋ฌธ๋จ ์")
table.set_cell_text(1, 1, str(len(document.paragraphs)))์์ฑ๋ ํ๋ ์ฒซ ๋ฒ์งธ ํ์ ํค๋์ฒ๋ผ ํ์ฉํ๊ณ , ๋ ๋ฒ์งธ ํ์ ํ์ฌ ๋ฌธ๋จ ์๋ฅผ ๊ธฐ๋กํฉ๋๋ค. ์ ํ ์คํธ๋ UTF-8 ๋ฌธ์์ด๋ก ๋ฐ๋ก ์ ๋ ฅํ๋ฉด ๋ฉ๋๋ค.
๋ฌธ์์ ํ
๋๋ฆฌ ์ฑ์ฐ๊ธฐ ์ ์๊ฐ ์๋ค๋ฉด add_table()์ด ์๋์ผ๋ก ๊ธฐ๋ณธ ์ค์ borderFill์ ํค๋์ ์ถ๊ฐํ๊ณ ํ/์
์ ์ฐธ์กฐ๋ฅผ ์ฑ์ ๋ฃ์ต๋๋ค.
๋ณํฉ๋ ํ๋ฅผ ๋ค๋ฃฐ ๋๋ `table.iter_grid()`/`table.get_cell_map()`์ผ๋ก ๋
ผ๋ฆฌ ์ขํ์ ์ค์ ์
์ ๋งคํํ ์ ์์ผ๋ฉฐ, `table.set_cell_text(..., logical=True, split_merged=True)`๋ก ๋
ผ๋ฆฌ ์ขํ ๊ธฐ๋ฐ ํธ์ง๊ณผ ๋ณํฉ ํด์ ๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
memo, paragraph, field_id = document.add_memo_with_anchor(
"๊ฒํ ์ค์
๋๋ค.",
paragraph=paragraph,
memo_shape_id_ref="0",
)
print("์ ๋ฉ๋ชจ ID:", memo.id)
print("ํ๋ ์ปจํธ๋กค ID:", field_id)memo_shape_id_ref๋ ๋ฌธ์ ํค๋์ ์ ์๋ ๋ฉ๋ชจ ๋ชจ์(ํ์ ๋ชจ์)์ ๊ฐ๋ฆฌํต๋๋ค. ๊ธฐ๋ณธ ํ
ํ๋ฆฟ์๋ ์ ์ด๋ ํ๋์ ๋ชจ์์ด ์ ์๋์ด ์์ผ๋ฏ๋ก "0"์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ฌธ์์ MEMO ์ปจํธ๋กค์ด ์์ผ๋ฉด ํ/๊ธ์์ ๋ฉ๋ชจ ํ์ ์ด ๋ณด์ด์ง ์์ต๋๋ค. `add_memo_with_anchor()`๋ ํ์ ์ปจํธ๋กค์ ์๋์ผ๋ก ๋ง๋ค์ด ์ฃผ๋ฏ๋ก ์ด๋ณด์๋ ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
output_path = "output/quickstart.hwpx"
document.save(output_path)
print("์ ์ฅ ์๋ฃ:", output_path)HwpxDocument.save()์ ๊ฒฝ๋ก๋ฅผ ๋๊ธฐ๋ฉด zip ๊ธฐ๋ฐ์ HWPX ์์นด์ด๋ธ๊ฐ ์์ฑ๋ฉ๋๋ค. ๋๋ ํฐ๋ฆฌ๊ฐ ์๋ค๋ฉด Path(output_path).parent.mkdir(parents=True, exist_ok=True)๋ก ๋จผ์ ์ค๋นํด ์ฃผ์ธ์.
- {doc}
usage์์ ๋ ๋ง์ ํธ์ง ํจํด๊ณผ ํ ๊ฐ์ฒด ์กฐ์ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ธฐ - {doc}
examples์ ์ ๋ฆฌ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ฉฐ ์ ์ฒด ์ํฌํ๋ก๋ฅผ ์ตํ๊ธฐ - XML ๊ตฌ์กฐ์ ๋งค๋ํ์คํธ๊ฐ ๊ถ๊ธํ๋ค๋ฉด {doc}
schema-overview๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๊ธฐ