import win32com.client as win32
import pandas as pd
def 한글_시작():
hwp = win32.gencache.EnsureDispatch("hwpframe.hwpobject")
hwp.XHwpWindows.Item(0).Visible = True
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
return hwp
if __name__ == '__main__':
hwp = 한글_시작()
hwp.Open(r"C:\Users\smj02\Desktop\표창장_서식.hwp")
df = pd.read_excel(r"C:\Users\smj02\Desktop\인적사항.xlsx")
hwp.Run("CopyPage")
for _ in range(len(df) - 1):
hwp.Run("PastePage")
field_list = ['연번', '회사', '직급', '이름', '내용']
for i in range(len(df)):
for field in field_list:
hwp.PutFieldText(f"{field}{{{{{i}}}}}", df[field][i])
(DRM 경우) win32.Dispatch("Excel.Application")을 이용하는 방법
import win32com.client as win32
import pandas as pd
def 한글_시작():
hwp = win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
hwp.XHwpWindows.Item(0).Visible = True
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
return hwp
def 엑셀_시작():
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = excel.Workbooks.Open(r"C:\Users\smj02\Desktop\인적사항.xlsx")
ws = wb.Worksheets(1)
excel.Visible = True
return excel, wb, ws
if __name__ == '__main__':
hwp = 한글_시작()
hwp.Open(r"C:\Users\smj02\Desktop\표창장_서식.hwp")
excel, wb, ws = 엑셀_시작()
ws.UsedRange.Select()
excel.Selection.Copy()
df = pd.read_clipboard()
hwp.Run("CopyPage")
for _ in range(len(df) - 1):
hwp.Run("PastePage")
field_list = ['연번', '회사', '직급', '이름', '내용']
for i in range(len(df)):
for field in field_list:
hwp.PutFieldText(f"{field}{{{{{i}}}}}", df[field][i])
df 대신 리스트를 사용하는 방법
import win32com.client as win32
import pandas as pd
def 한글_시작():
hwp = win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
hwp.XHwpWindows.Item(0).Visible = True
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
return hwp
def 엑셀_시작():
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = excel.Workbooks.Open(r"C:\Users\smj02\Desktop\인적사항.xlsx")
ws = wb.Worksheets(1)
excel.Visible = True
return excel, wb, ws
if __name__ == '__main__':
hwp = 한글_시작()
hwp.Open(r"C:\Users\smj02\Desktop\표창장_서식.hwp")
excel, wb, ws = 엑셀_시작()
data = ws.UsedRange()[1:]
data_list = [[i[0], i[1], i[2], i[6], i[9]] for i in data]
hwp.Run("CopyPage")
for _ in range(len(data) - 1):
hwp.Run("PastePage")
field_list = ['연번', '회사', '직급', '이름', '내용']
for idx, field in enumerate(field_list):
for i in range(len(data)):
hwp.PutFieldText(f"{field}{{{{{i}}}}}", data_list[i][idx])
df 대신 dict를 이용하는 방법
from time import sleep
import win32com.client as win32
import pandas as pd
def 한글_시작():
hwp = win32.gencache.EnsureDispatch("HWPFrame.HwpObject")
hwp.XHwpWindows.Item(0).Visible = True
hwp.RegisterModule("FilePathCheckDLL", "FilePathCheckerModule")
return hwp
def 엑셀_시작():
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = excel.Workbooks.Open(r"C:\Users\smj02\Desktop\인적사항.xlsx")
ws = wb.Worksheets(1)
excel.Visible = True
return excel, wb, ws
if __name__ == '__main__':
hwp = 한글_시작()
hwp.Open(r"C:\Users\smj02\Desktop\표창장_서식.hwp")
excel, wb, ws = 엑셀_시작()
data = list(zip(*ws.UsedRange()))
key_list = [i[0] for i in data]
val_list = [i[1:] for i in data]
data_zip = list(zip(key_list, val_list))
data_dict = {i: j for i, j in data_zip}
hwp.Run("CopyPage")
for _ in range(len(data[0]) - 2):
hwp.Run("PastePage")
field_list = ['연번', '회사', '직급', '이름', '내용']
for idx, field in enumerate(field_list):
for i in range(len(data[0]) - 1):
hwp.PutFieldText(f"{field}{{{{{i}}}}}", data_dict[field][i])
pandas의 DataFrame을 이용하는 방법(추천)
(DRM 경우) win32.Dispatch("Excel.Application")을 이용하는 방법
df 대신 리스트를 사용하는 방법
df 대신 dict를 이용하는 방법
샘플파일 링크
설명은 동영상으로 남겨보았습니다.
데이터프레임으로 표창장 채우기
엑셀파일에 DRM이 걸려 있는 경우 표창장 채우기(업로드 예정)