안녕하세요?? 한글에서 스크립트 녹화 후 아래코드로 변환해서 실행하는데, 오류가발생합니다…
문서 내 존재하는 표의 갯수만큼 매크로를 반복하는 코드인데… 뭐가문제인지 모르겠네요…
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)