표안에 셀 값 넣기 오류


def insert_cell(table_index, row, col, value):
    try:
        table_ctrl = Table[table_index]
        hwp.SetPosBySet(table_ctrl.GetAnchorPos(0))
        # hwp.FindCtrl()
        hwp.Run("SelectCtrlReverse")  # 표 안에 커서 위치
        hwp.Run("ShapeObjTableSelCell")  # 표 안에 커서 위치
        
        # 원하는 셀로 이동
        for _ in range(row):
            hwp.Run("TableLowerCel")
        for _ in range(col):
            hwp.Run("TableRightCell")

        hwp.Run("TableEditCell")

        # InsertText 액션 사용
        action = "InsertText"
        hwp.HAction.GetDefault(action, hwp.HParameterSet.HInsertText.HSet)
        hwp.HParameterSet.HInsertText.Text = str(value) if value is not None else ""
        hwp.HAction.Execute(action, hwp.HParameterSet.HInsertText.HSet)
        print(f"{table_index}번 표 - ({row},{col}): {value}")

    except Exception as e:
        print(f"오류 발생 (표: {table_index}, 셀: {row}, {col}): {e}")

표안에 셀값을 넣으려고 하는데, 표 밖에 값이 입력되는데 어떻게 해야 할까요??? ㅜㅜ 한글 2020 버전입니다.

안녕하세요

아래 코드에 우선 오타 있습니다.

hwp.Run("TableLowerCel")

TableLowerCell로 변경했습니다.

Table[table_index]는 어떻게 구현 하셨나요?
table_ctrl = Table[table_index] 만 아래 코드로 변경해서 테스트 해보니 정상 동작 합니다.

def insert_cell(table_index, row, col, value):
    try:
        tblCnt = 0
        table_ctrl = hwp.HeadCtrl
        while table_ctrl :
            strId = table_ctrl.CtrlID
            if strId == "tbl" :
                tblCnt+=1
                if tblCnt == table_index :
                    break                
            table_ctrl = table_ctrl.Next

        hwp.SetPosBySet(table_ctrl.GetAnchorPos(0))
        # hwp.FindCtrl()
        hwp.Run("SelectCtrlReverse")  # 표 안에 커서 위치
        hwp.Run("ShapeObjTableSelCell")  # 표 안에 커서 위치
        
        # 원하는 셀로 이동
        for _ in range(row):
            hwp.Run("TableLowerCell")
        for _ in range(col):
            hwp.Run("TableRightCell")

        hwp.Run("TableEditCell")

        # InsertText 액션 사용
        action = "InsertText"
        hwp.HAction.GetDefault(action, hwp.HParameterSet.HInsertText.HSet)
        hwp.HParameterSet.HInsertText.Text = str(value) if value is not None else ""
        hwp.HAction.Execute(action, hwp.HParameterSet.HInsertText.HSet)
        print(f"{table_index}번 표 - ({row},{col}): {value}")

    except Exception as e:
        print(f"오류 발생 (표: {table_index}, 셀: {row}, {col}): {e}")

다시 확인해보시고 추가 문의사항 있으면 다시 말씀부탁드립니다.

참고로 저는 hwp.XHwpWindows.Active_XHwpWindow.Visible = True 로 하고
파이썬 코드를 한 줄 씩 디버깅하면서 한글 앱과 비교하면서 문제를 찾았습니다. :grinning_face:

감사합니다.

2개의 좋아요

감사합니다. 많은 도움이 되었습니다. :+1: 표 내용 컨트롤 하고 나서 많은 진전이 있었습니다.

1개의 좋아요