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

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

by Jade S. 2025. 1. 21.
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 = """도널드 트럼프(Donald Trump)는 45대 미국 대통령으로 잘 알려져 있으며, 그가 정치와 사회에 미친 영향은 매우 크고 복잡합니다. 트럼프는 1946년 6월 14일, 뉴욕에서 태어났으며, 부유한 부동산 개발자로 시작하여 텔레비전 쇼와 광고를 통해 대중적인 인기를 얻었습니다. 그가 정치에 뛰어든 것은 2015년 공화당 대선 후보로 출마하면서부터입니다.

트럼프는 정치적 경력이 거의 없었지만, 그의 기업인 이미지와 강한 언행이 대중의 주목을 끌었고, 이를 통해 미국 정치에 신선한 충격을 주었습니다. 그가 대통령 선거에 출마할 당시, 그의 주장은 경제적 보호주의, 이민자에 대한 강경한 입장, 자유 시장 개혁 등이었고, 많은 이들이 그를 '일반적인 정치인과는 다른 인물'이라고 평되었습니다."""

# 텍스트 박스 설정
text_box_x = 226
text_box_y = 5334
text_box_width = 4156  # 텍스트 박스 폭
text_box_height = 1021  # 텍스트 박스 높이

# 초기 글자 크기와 줄 간격 설정
font_size = 100
line_spacing = 20

# 텍스트가 텍스트 박스에 맞게 조정
while True:
    font = ImageFont.truetype(font_path, font_size)
    wrapped_text = []
    current_line = ""
    for word in feedback_text.split():
        test_line = f"{current_line} {word}".strip()
        bbox = font.getbbox(test_line)  # getbbox로 텍스트 크기 계산
        text_width = bbox[2] - bbox[0]  # 텍스트의 가로 크기
        if text_width <= text_box_width:
            current_line = test_line
        else:
            wrapped_text.append(current_line)
            current_line = word
    wrapped_text.append(current_line)

    # 전체 텍스트 높이 계산
    total_height = sum(font.getbbox(line)[3] - font.getbbox(line)[1] + line_spacing for line in wrapped_text) - line_spacing

    if total_height > text_box_height:  # 텍스트가 박스를 넘는 경우
        font_size -= 2
        line_spacing = max(10, line_spacing - 1)  # 줄 간격 최소값 제한
    elif total_height < text_box_height * 0.9:  # 텍스트가 박스를 충분히 채우지 못하는 경우
        font_size += 2
        line_spacing += 1
    else:
        break  # 텍스트가 박스에 적절히 맞으면 종료

# 텍스트 삽입
draw = ImageDraw.Draw(template)
y_position = text_box_y
for line in wrapped_text:
    draw.text((text_box_x, y_position), line, font=font, fill="black")
    y_position += font.getbbox(line)[3] - font.getbbox(line)[1] + line_spacing

# 그래프 크기와 위치 설정
grade_graph = grade_graph.resize((4234, 2142))
progress_graph = progress_graph.resize((4270, 1356))

template.paste(grade_graph, (196, 992))
template.paste(progress_graph, (178, 3511))

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

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

 

>> 결과물

>> 개선의 여지

텍스트 들여쓰기 안됨.

그래프를 이미지 말고 데이터로 바로 처리 하면 좋겠다.

여러명을 한꺼번에 처리하면 편하겠네.

 

>> 결론

노가다.

결과가 바로 보여서 재밌긴 한데 시간을 너무 많이 잡아 먹음.

728x90
반응형