hwp 파이썬 실행오류 문의

안녕하세요?? 한글에서 스크립트 녹화 후 아래코드로 변환해서 실행하는데, 오류가발생합니다…

문서 내 존재하는 표의 갯수만큼 매크로를 반복하는 코드인데… 뭐가문제인지 모르겠네요…

def count_tables(hwp):
try:
table_count = 0
ctrl = hwp.HeadCtrl # 첫 컨트롤 가져오기
while ctrl:
if ctrl.CtrlID == “tbl”: # CtrlID가 “tbl"인 경우
table_count += 1
ctrl = ctrl.Next # 다음 컨트롤로 이동
print(f"문서 내 테이블 개수: {table_count}”)
return table_count
except Exception as e:
print(f"테이블 카운트 중 오류 발생: {e}")
return 0

def run_macro(hwp, table_count):
for i in range(table_count):
# Goto action
hwp.HAction.GetDefault(“Goto”, hwp.HParameterSet.HGotoE.HSet)
hwp.HParameterSet.HGotoE.HSet.SetItem(“DialogResult”, 55)
hwp.HParameterSet.HGotoE.SetSelectionIndex = 5
hwp.HAction.Execute(“Goto”, hwp.HParameterSet.HGotoE.HSet)

    # FindCtrl action
    hwp.FindCtrl()

    # TablePropertyDialog action
    hwp.HAction.GetDefault("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet)
    #hwp.HParameterSet.HShapeObject.RepeatHeader = 0
    hwp.HParameterSet.HShapeObject.PageBreak = hwp.TableBreak("Table")
    hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeType", 6)
    hwp.HAction.Execute("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet)

    # Adjust Table Cells
    hwp.HAction.Run("MoveDown")
    hwp.HAction.Run("ShapeObjTableSelCell")
    hwp.HAction.Run("TableCellBlock")
    hwp.HAction.Run("TableCellBlockExtend")
    hwp.HAction.Run("TableCellBlockExtend")

    # CellBorder action
    hwp.HAction.GetDefault("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet)
    hwp.HParameterSet.HCellBorderFill.WidthVert = hwp.HwpLineWidth("0.1mm")
    hwp.HParameterSet.HCellBorderFill.TypeHorz = hwp.HwpLineType("Solid")
    hwp.HParameterSet.HCellBorderFill.WidthHorz = hwp.HwpLineWidth("0.1mm")
    hwp.HParameterSet.HCellBorderFill.BorderWidthBottom = hwp.HwpLineWidth("0.1mm")
    hwp.HParameterSet.HCellBorderFill.BorderWidthTop = hwp.HwpLineWidth("0.1mm")
    hwp.HParameterSet.HCellBorderFill.BorderWidthRight = hwp.HwpLineWidth("0.1mm")
    hwp.HParameterSet.HCellBorderFill.BorderWidthLeft = hwp.HwpLineWidth("0.1mm")
    hwp.HAction.Execute("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet)

    # ParagraphShape action
    hwp.HAction.GetDefault("ParagraphShape", hwp.HParameterSet.HParaShape.HSet)
    hwp.HParameterSet.HParaShape.LineSpacingType = hwp.LineSpacingMethod("Percent")
    hwp.HParameterSet.HParaShape.LineSpacing = 100
    hwp.HParameterSet.HParaShape.BreakNonLatinWord = 0
    hwp.HAction.Execute("ParagraphShape", hwp.HParameterSet.HParaShape.HSet)

 Table Resize
for _ in range(100):  # Simulates multiple "TableResizeExUp" actions
    hwp.HAction.Run("TableResizeExUp")
hwp.RunScriptMacro("OnScriptMacro_자동전처리()", 0, 0)

안녕하세요.

직접 작성하신 코드가 아니신지요?

파이썬 언어의 특성상 코드 인덴트가 중요한데 맞춰있지 않은 라인들이 있고,
주석으로 처리해야 할 라인에 주석이 빠진 부분도 있고요,
“OnScriptMacro_자동전처리()” 스크립트를 실행하는 코드가 있으니 실행 환경에 해당 스크립트도 있는지 확인도 필요하고요.

위의 조건들이 맞춰지면 코드 실행이 가능할 것으로 보입니다.

def count_tables(hwp):
    try:
        table_count = 0
        ctrl = hwp.HeadCtrl # 첫 컨트롤 가져오기
        while ctrl:
            if ctrl.CtrlID == "tbl": # CtrlID가 "tbl"인 경우
                table_count += 1
            ctrl = ctrl.Next # 다음 컨트롤로 이동
        print(f"문서 내 테이블 개수: {table_count}")
        return table_count
    except Exception as e:
        print(f"테이블 카운트 중 오류 발생: {e}")
        return 0

def run_macro(hwp, table_count):
    for i in range(table_count):
        # Goto action
        hwp.HAction.GetDefault("Goto", hwp.HParameterSet.HGotoE.HSet)
        hwp.HParameterSet.HGotoE.HSet.SetItem("DialogResult", 55)
        hwp.HParameterSet.HGotoE.SetSelectionIndex = 5
        hwp.HAction.Execute("Goto", hwp.HParameterSet.HGotoE.HSet)

        # FindCtrl action
        hwp.FindCtrl()

        # TablePropertyDialog action
        hwp.HAction.GetDefault("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet)
        #hwp.HParameterSet.HShapeObject.RepeatHeader = 0
        hwp.HParameterSet.HShapeObject.PageBreak = hwp.TableBreak("Table")
        hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeType", 6)
        hwp.HAction.Execute("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet)

        # Adjust Table Cells
        hwp.HAction.Run("MoveDown")
        hwp.HAction.Run("ShapeObjTableSelCell")
        hwp.HAction.Run("TableCellBlock")
        hwp.HAction.Run("TableCellBlockExtend")
        hwp.HAction.Run("TableCellBlockExtend")

        # CellBorder action
        hwp.HAction.GetDefault("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet)
        hwp.HParameterSet.HCellBorderFill.WidthVert = hwp.HwpLineWidth("0.1mm")
        hwp.HParameterSet.HCellBorderFill.TypeHorz = hwp.HwpLineType("Solid")
        hwp.HParameterSet.HCellBorderFill.WidthHorz = hwp.HwpLineWidth("0.1mm")
        hwp.HParameterSet.HCellBorderFill.BorderWidthBottom = hwp.HwpLineWidth("0.1mm")
        hwp.HParameterSet.HCellBorderFill.BorderWidthTop = hwp.HwpLineWidth("0.1mm")
        hwp.HParameterSet.HCellBorderFill.BorderWidthRight = hwp.HwpLineWidth("0.1mm")
        hwp.HParameterSet.HCellBorderFill.BorderWidthLeft = hwp.HwpLineWidth("0.1mm")
        hwp.HAction.Execute("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet)

        # ParagraphShape action
        hwp.HAction.GetDefault("ParagraphShape", hwp.HParameterSet.HParaShape.HSet)
        hwp.HParameterSet.HParaShape.LineSpacingType = hwp.LineSpacingMethod("Percent")
        hwp.HParameterSet.HParaShape.LineSpacing = 100
        hwp.HParameterSet.HParaShape.BreakNonLatinWord = 0
        hwp.HAction.Execute("ParagraphShape", hwp.HParameterSet.HParaShape.HSet)

        # Table Resize
        for _ in range(100):  # Simulates multiple "TableResizeExUp" actions
            hwp.HAction.Run("TableResizeExUp")
        hwp.RunScriptMacro("OnScriptMacro_자동전처리()", 0, 0)

감사합니다.