강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

sunstephan님의 프로필 이미지
sunstephan

작성한 질문수

직장인에게 꼭 필요한 파이썬-아래아한글 자동화 레시피

날짜 뒤에 요일 붙이기

날짜 뒤에 요일 붙이기 질문

해결된 질문

작성

·

350

1

import os
import datetime as dt
from time import sleep

import win32com.client as win32

def init_hwp():
    hwp = win32.gencache.EnsureDispatch("hwpframe.hwpobject")
    hwp.XHwpWindows.Item(0).Visible = True
    hwp.RegisterModule("FilePathCheckDLL","FilePathCheckerModule")
    return hwp

hwp = init_hwp()
#hwp.Open(r"D:\\SHW\\PROGRAM\\PYTHON\\hwp\\data\\실작업공정표.hwpx")
hwp.Open(os.path.join(os.getcwd(), r"D:\\SHW\\PROGRAM\\PYTHON\\hwp\\data\\실작업공정표.hwpx"))

def get_value():
    hwp.InitScan(Range=0xff)  # 추출범위를 "선택영역"으로 지정
    text = hwp.GetText()[1]  # 선택범위 문자열값 추출
    hwp.ReleaseScan()  # 검색종료
    return text  # 추출값 리턴


def get_weekday(text):
    week_list = ["월", "화", "수", "목", "금", "토", "일"]
    month, day = [int(i) for i in text.split(".")[:2]]
    week_num = dt.date(2022, month, day).weekday()
    week_day = week_list[week_num]
    return f"({week_day})"


def insert_text(text):
    act = hwp.CreateAction("InsertText")
    pset = act.CreateSet()
    pset.SetItem("Text", text)
    act.Execute(pset)

if __name__ == '__main__':
    hwp.FindCtrl()  # 표 선택
    hwp.Run("ShapeObjTableSelCell")  # 첫 번째 셀로 진입(셀선택상태)
    while True:  # 무한반복
        text = get_value()  # 셀의 텍스트 추출
        if text.endswith("."):  # 셀 안의 텍스트가 "."으로 끝나면(날짜셀을 구분하는 임의의 방법)
            weekday = get_weekday(text)  # 요일 파악
            hwp.Run("Cancel")  # 셀선택 취소
            hwp.Run("MoveLineEnd")  # 문자열 끝으로 이동
            insert_text(weekday)  # 요일 삽입
            hwp.Run("TableCellBlock")  # 다시 셀선택
        if not hwp.HAction.Run("TableRightCell"):  # 우측으로 이동하다
            break  # 끝에 도달하면 while문 종료

 

아무리 해도 문서에서 변경이 안일어나는데 뭐가 문제인지 모르겠습니다.

그리고 마지막 if 문에서

if __name__ == '__main__':

이게 무슨 의미인지 설명 부탁 드립니다.

답변 1

1

일코님의 프로필 이미지
일코
지식공유자

sunstephan님 오랜만에 인사드립니다^^

해당 문서를 메일로 한 번 보내주시겠어요?
(날짜서식 이외에 민감한 부분은 삭제해주셔도 됩니다.)

예제문서의 날짜서식과 sunstephan님의 문서 날짜서식이 달라서 그런 것으로 추정됩니다.

martinii.fun@gmail.com으로 보내주시면 예시코드를 짜서 보내드려보겠습니다.

감사합니다. 행복한 하루 되세요^^

sunstephan님의 프로필 이미지
sunstephan
질문자

처음에 셀블럭을 선택하게 한 다음에 실행하니 작동 되네요..

5번째 줄에서 시작할 때 블럭으로 잡은 다음에 텍스트를 추출할 수 있는거 같아요.

if __name__ == '__main__':
    hwp.FindCtrl()  # 표 선택
    hwp.Run("ShapeObjTableSelCell")  # 첫 번째 셀로 진입(셀선택상태)
    while True:  # 무한반복
        hwp.Run("TableCellBlock")  #! 셀 블럭 선택하기
        text = get_value()  # 셀의 텍스트 추출
        if text.endswith("."):  # 셀 안의 텍스트가 "."으로 끝나면(날짜셀을 구분하는 임의의 방법)
            weekday = get_weekday(text)  # 요일 파악
            hwp.Run("Cancel")  # 셀선택 취소
            hwp.Run("MoveLineEnd")  # 문자열 끝으로 이동
            insert_text(weekday)  # 요일 삽입
            hwp.Run("TableCellBlock")  # 다시 셀선택
        if not hwp.HAction.Run("TableRightCell"):  # 우측으로 이동하다
            break  # 끝에 도달하면 while문 종료
sunstephan님의 프로필 이미지
sunstephan

작성한 질문수

질문하기