한글API를 이용하여 표의 행을 복사해서 밑에 붙이는방법

안녕하세요
한글API를 사용하여 스크립트로 동적으로 표를 생성 및 데이터를 넣는 작업을 진행하고있습니다.

관련해서 진행중에 표의 헤더가있고 데이터가 되는 행이 1줄만 있었다는 가정하에,
행을 동적으로 n개만큼 복사해서 기존 표 밑에 붙이는방법이 있을까요?

2개의 좋아요

안녕하세요,
kmryu님이 말씀하신대로 붙여넣는 작업은 웹한글 API를 이용해서는 어려울것 같습니다.
아니면 아래와 같이 데이터행의 제일 끝에서 TableRightCellAppend 액션을 실행하면 아래에 행이 추가됩니다.
그이후 데이터를 하나씩 생성해서 넣는 방식은 어떨까요 …?
감사합니다 :slight_smile:

3개의 좋아요

답변 주셔서 감사합니다.
말씀해주신 방안으로 고려를 해봤지만, 데이터의 행이 아래와 같이 병합되어있는 케이스가 있는상태입니다.

일반 한글파일에서 해당 행에 커서를 두고 복사/붙여넣기를 아래쪽으로 하는 경우에 대하여 구현이 가능한가해서 여쭸었습니다. 가이드주신 “TableRightCellAppend” 방식으로는 병합행 구현까지는 어렵겠죠?

2개의 좋아요

네 바로 한줄은 추가 가능하지만 … 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");
3개의 좋아요

가이드주신 대로 테스트적용 해보도록 하겠습니다.
바쁘실텐데 가이드 지원 감사드립니다!

2개의 좋아요

bhjung님 답변으로 해결하셨겠지만,
파이썬으로 진행중이시라면 아래 방법도 검토해 보시기 바랍니다^^
파이썬 모듈 pyhwpx의 편의 메서드를 이용한 방식입니다.

시연화면은 아래와 같으며,
녹화_2024_01_14_15_21_27_718

코드는 아래와 같습니다.

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("끝!")

도움이 되었길 바랍니다^^

2개의 좋아요