본문 바로가기
코딩 수업/파이썬 (업무자동화)

[업무자동화] Monthly Report 만드는 프로그램 (파이썬 코드, Python Program to Create a Monthly Report, 월간 리포트, 피드백, 성적표 만들기)

by Jade S. 2025. 1. 20.
728x90
반응형
from PIL import Image, ImageDraw, ImageFont

# 파일 경로 설정
template_path = "C:/Users/MJ/Desktop/월간리포트만들기/template.png"  # 포토샵 템플릿 이미지
grade_graph_path = "C:/Users/MJ/Desktop/월간리포트만들기/bg.png"  # 성적 그래프 이미지
progress_graph_path = "C:/Users/MJ/Desktop/월간리포트만들기/bp.png"  # 진도 그래프 이미지
font_path = "C:/Users/MJ/Desktop/월간리포트만들기/NanumGothic.ttf"  # 한글 폰트

# 이미지 열기
template = Image.open(template_path)
grade_graph = Image.open(grade_graph_path)
progress_graph = Image.open(progress_graph_path)

# 텍스트 피드백 준비
feedback_text = """텍스트 피드백"""

# 이미지 크기와 위치 설정
template_width, template_height = template.size

# 성적 그래프 크기: 542x228
grade_graph = grade_graph.resize((553, 244))

# 진도 그래프 크기: 537x158
progress_graph = progress_graph.resize((553, 174))

# 이미지에 텍스트 추가 (폰트 지정)
draw = ImageDraw.Draw(template)

# 첫 번째 이미지 (성적 그래프) 삽입 위치
template.paste(grade_graph, (22, 126))

# 두 번째 이미지 (진도 그래프) 삽입 위치
template.paste(progress_graph, (20, 421))

# 텍스트 박스 영역 설정 (좌표와 크기)
text_box_x = 23
text_box_y = 647
text_box_width = 548  # 가로 폭을 548픽셀로 설정
text_box_height = 168  # 높이

# 텍스트가 들어갈 수 있는 최대 글자 크기 계산
font_size = 40  # 글자 크기 (다시 큰 값으로 설정)
font = ImageFont.truetype(font_path, font_size)

# 텍스트가 상자에 맞지 않으면 글자 크기 조정
while True:
    # 텍스트 줄바꿈 처리 (각 줄이 텍스트 박스를 넘지 않도록)
    wrapped_text = ""
    lines = feedback_text.split("\n")
    for line in lines:
        current_line = ""
        words = line.split(" ")
        for word in words:
            # 현재 줄에 단어를 추가했을 때 텍스트 박스의 폭을 넘지 않으면 추가
            test_line = current_line + word + " "
            bbox = draw.textbbox((0, 0), test_line, font=font)  # 텍스트 크기 계산
            text_width = bbox[2] - bbox[0]  # 텍스트의 가로 크기
            if text_width <= text_box_width:
                current_line = test_line
            else:
                # 넘으면 줄바꿈 처리
                wrapped_text += current_line.strip() + "\n"
                current_line = word + " "
        wrapped_text += current_line.strip() + "\n"  # 마지막 줄 추가
   
    # 텍스트 크기 계산
    bbox = draw.textbbox((0, 0), wrapped_text, font=font)
    text_width = bbox[2] - bbox[0]  # 텍스트의 가로 크기
    text_height = bbox[3] - bbox[1]  # 텍스트의 세로 크기
   
    # 텍스트 크기가 상자에 맞으면 반복 종료
    if text_width <= text_box_width and text_height <= text_box_height:
        break
   
    # 글자 크기 줄이기
    font_size -= 2
    font = ImageFont.truetype(font_path, font_size)

# 최종 글자 크기에 맞게 텍스트 삽입
draw.text((text_box_x, text_box_y), wrapped_text, font=font, fill=(0, 0, 0))

# PDF로 저장
pdf_path = "C:/Users/MJ/Desktop/월간리포트만들기/MonthlyReport.pdf"
template.save(pdf_path, "PDF", resolution=300.0)

print("성적표 PDF가 생성되었습니다.")

 

>> 결과물

728x90
반응형