안녕하세요
한글API를 사용하여 스크립트로 동적으로 표를 생성 및 데이터를 넣는 작업을 진행하고있습니다.
관련해서 진행중에 표의 헤더가있고 데이터가 되는 행이 1줄만 있었다는 가정하에,
행을 동적으로 n개만큼 복사해서 기존 표 밑에 붙이는방법이 있을까요?
안녕하세요
한글API를 사용하여 스크립트로 동적으로 표를 생성 및 데이터를 넣는 작업을 진행하고있습니다.
관련해서 진행중에 표의 헤더가있고 데이터가 되는 행이 1줄만 있었다는 가정하에,
행을 동적으로 n개만큼 복사해서 기존 표 밑에 붙이는방법이 있을까요?
네 바로 한줄은 추가 가능하지만 … kmryu님 같은 케이스로 구현을 한다면 아래 코드와 같이 구현은 가능할것 같습니다 …!
코드가 조금 기네요 ㅠ…!
HwpCtrl.Run("TableRightCellAppend");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("TableAppendRow");
HwpCtrl.Run("TableRightCellAppend");
HwpCtrl.Run("TableCellBlock");
HwpCtrl.Run("TableUpperCell");
HwpCtrl.Run("TableCellBlockExtend");
HwpCtrl.Run("TableLowerCell");
HwpCtrl.Run("TableMergeCell");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("TableCellBlock");
HwpCtrl.Run("TableCellBlockExtend");
HwpCtrl.Run("TableLowerCell");
HwpCtrl.Run("TableMergeCell");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("TableCellBlock");
HwpCtrl.Run("TableCellBlockExtend");
HwpCtrl.Run("TableLowerCell");
HwpCtrl.Run("TableMergeCell");
HwpCtrl.Run("MoveLeft");
HwpCtrl.Run("MoveLeft");
HwpCtrl.Run("MoveLeft");
HwpCtrl.Run("MoveLeft");
HwpCtrl.Run("MoveLeft");
var tbact = HwpCtrl.CreateAction("InsertText");
var tbset = tbact.CreateSet();
tbact.GetDefault(tbset);
tbset.SetItem("Text", "1");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "2");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "3");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "4");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "5");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "6");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "7");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "8");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
tbset.SetItem("Text", "9");
tbact.Execute(tbset);
HwpCtrl.Run("MoveRight");
가이드주신 대로 테스트적용 해보도록 하겠습니다.
바쁘실텐데 가이드 지원 감사드립니다!
bhjung님 답변으로 해결하셨겠지만,
파이썬으로 진행중이시라면 아래 방법도 검토해 보시기 바랍니다^^
파이썬 모듈 pyhwpx의 편의 메서드를 이용한 방식입니다.
시연화면은 아래와 같으며,

코드는 아래와 같습니다.
from pyhwpx import Hwp
import pandas as pd
hwp = Hwp()
hwp.open("./빈 문서 1.hwp")
df = pd.read_excel("./데이터.xlsx").head(20)
# 표 안으로 들어가서
hwp.get_into_nth_table(0, select=True)
# 표에서 반복될 셀만 선택
hwp.TableLowerCell()
hwp.TableCellBlockExtend()
hwp.TableColEnd()
hwp.TableColPageDown()
# 잘라내기(반복붙여넣기 위함)
hwp.Cut(remove_cell=True)
hwp.CloseEx()
# 잘라낸 셀을 df 행 갯수만큼 붙여넣기
hwp.MoveDocEnd()
for i in range(len(df)):
hwp.Paste()
# 필드 채우기(문서 내 필드 목록과 df의 칼럼명이 일치하면 자동 매치됨)
hwp.put_field_text(df)
# 다시 0번 표(제목표)로 커서를 옮긴 후 모든 표 병합하기
hwp.get_into_nth_table(0)
while hwp.TableMergeTable():
pass
print("끝!")
도움이 되었길 바랍니다^^