모아찍기가 기본으로 설정된 문서를 포함해서 모든 문서를 한페이지당 하나씩 인쇄하는 옵션으로 hwp파일을 pdf로 변환해주는 automation python코드를 작성하려고 합니다.
코드는 아래와 같은데, 해당 코드를 실행하면 “인쇄할 쪽 번호가 없습니다” 오류가 발생합니다.
비슷한 오류를 겪었거나, 해결 방법을 아시는 분이 계시다면 도움이 필요합니다…
승승아빠-한글매크로를 기반으로 작성한 코드입니다.
def convert_hwp_to_pdf(self, hwp_path, pdf_path):
pythoncom.CoInitialize()
hwp = None
try:
hwp = win32com.client.gencache.EnsureDispatch('HWPFrame.HwpObject')
hwp.XHwpWindows.Item(0).Visible = True
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
hwp.Open(hwp_path, "", "forceopen:true;versionwarning:false;")
hwp.Run("MoveDocEnd")
time.sleep(0.5)
hwp.Run("MoveDocBegin")
time.sleep(0.5)
\# ---------------------------------------------------------
\# \[핵심 조치\] 3. 페이지가 0인지 체크 (빈 문서 방어)
\# ---------------------------------------------------------
if hwp.PageCount == 0:
print(f"⚠️ 경고: {hwp_path} 파일의 페이지가 0쪽입니다. 인쇄를 건너뜁니다.")
return # 빈 문서이므로 인쇄하지 않고 함수 종료
\# HWP 팝업창 무시 (매우 중요!)
\# 0x10000 = 사용자 입력 대기 안 함, 0x20000 = 경고창 표시 안 함
hwp.SetMessageBoxMode(0x10000 | 0x20000)
hwp.HAction.Run("AcceptAllRevisions") # 모든 변경 사항 반영
hwp.HAction.Run("TrackChanges") # 변경 추적 기능 끄기
\# -----------------------------
\# 인쇄 설정 초기화
\# -----------------------------
hwp.Run("MoveDocBegin")
time.sleep(1)
hwp.HAction.GetDefault("Print", hwp.HParameterSet.HPrint.HSet)
\# ===== 매크로 설정 그대로 적용 =====
\# 인쇄 방식 (1 = 공급 용지 맞춤)
hwp.HParameterSet.HPrint.PrintMethod = 1
\# 부수 정렬
hwp.HParameterSet.HPrint.Collate = 1
hwp.HParameterSet.HPrint.UserOrder = 0
\# 파일로 출력
hwp.HParameterSet.HPrint.PrintToFile = 1
\# 전체 문서
hwp.HParameterSet.HPrint.Range = 0
\# PDF 프린터
hwp.HParameterSet.HPrint.PrinterName = "Microsoft Print to PDF"
\# 출력 파일
hwp.HParameterSet.HPrint.filename = pdf_path
\# 페이지 번호 사용
hwp.HParameterSet.HPrint.UsingPagenum = 0
hwp.HParameterSet.HPrint.ReverseOrder = 0
hwp.HParameterSet.HPrint.Pause = 0
hwp.HParameterSet.HPrint.PrintImage = 1
hwp.HParameterSet.HPrint.PrintDrawObj = 1
hwp.HParameterSet.HPrint.PrintClickHere = 0
hwp.HParameterSet.HPrint.PrintAutoFootnoteLtext = ""
hwp.HParameterSet.HPrint.PrintAutoFootnoteCtext = ""
hwp.HParameterSet.HPrint.PrintAutoFootnoteRtext = ""
hwp.HParameterSet.HPrint.PrintAutoHeadnoteLtext = ""
hwp.HParameterSet.HPrint.PrintAutoHeadnoteCtext = ""
hwp.HParameterSet.HPrint.PrintAutoHeadnoteRtext = ""
hwp.HParameterSet.HPrint.PrintFormObj = 1
hwp.HParameterSet.HPrint.PrintMarkPen = 0
hwp.HParameterSet.HPrint.PrintMemo = 0
hwp.HParameterSet.HPrint.PrintMemoContents = 0
hwp.HParameterSet.HPrint.PrintRevision = 1
hwp.HParameterSet.HPrint.PrintBarcode = 1
hwp.HParameterSet.HPrint.Flags = 8192
\# Device = Printer
hwp.HParameterSet.HPrint.Device = 0
hwp.HParameterSet.HPrint.PrintPronounce = 0
hwp.HParameterSet.HPrint.NumCopy = 0
\# -----------------------------
\# 인쇄 실행
\# -----------------------------
hwp.HAction.Execute("Print", hwp.HParameterSet.HPrint.HSet)
time.sleep(2)
\# time.sleep(2) 대신 파일이 생성될 때까지 대기
timeout = 2
elapsed = 0
while not os.path.exists(pdf_path) or os.path.getsize(pdf_path) == 0:
time.sleep(1)
elapsed += 1
if elapsed > timeout:
break
finally:
if hwp:
hwp.Clear(1) # 종료 시 저장 여부를 묻는 팝업 방지
hwp.Quit()
pythoncom.CoUninitialize()