[업무 자동화]/파이썬

[파이썬] MS office 자동화 실습(feat. 엑셀, PPT, 워드클라우드)

시간 확보러 2024. 9. 8. 08:00
728x90

이번 실습은 여러 자료를 엑셀 취합하고, 워드클라우드 등을 통해 각각 상품별 PPT 작성이다.

책에서 언급하고 있는 대로 실습을 하면 크게 오류는 없다.

결과물은 아래와 같이 각각 상품별로 PPT가 자동 만들어 진다.

각 상품별 최신 트렌드를 아래와 같이 정리하기에는 좋을 것으로 보인다.

 

예를 들어, 비트코인, 미국 주식 등의 트렌드를 한번에 파악 할 수 있을 것으로 보인다.

 

 

※ 작성 코드

import openpyxl

wb=openpyxl.Workbook()

wb.create_sheet('car',0)
wb.create_sheet('semiconductor',0)
wb.create_sheet('metaverse',0)
wb.create_sheet('battery',0)

wb.save('product_info.xlsx')

 

path="practice/참고자료/"
import os
file_list=os.listdir(path)

 

file_list_xls=[]
for a in file_list:
    if ".xlsx" in a:
        file_list_xls.append(a)
file_list=file_list_xls

 

import pandas as pd

for a in file_list:
    df = pd.read_excel(f"practice/참고자료/{a}")
    print(df)

 

import pandas as pd
import os
path_dir = "practice/참고자료/"
files=os.listdir(path_dir)
files2=[]
for a in files:
    if '.xlsx' in a:
        files2.append(a)
files=files2

file_nm="product_info.xlsx"
with pd.ExcelWriter(file_nm) as writer:
    for file_name in files:
        df = pd.read_excel("practice/참고자료/" + file_name)
        df.to_excel(writer, sheet_name=file_name.replace('.xlsx',''))

 

import pandas as pd
import os
path_dir = "practice/참고자료/"
files=os.listdir(path_dir)
files2=[]
for a in files:
    if '.xlsx' in a:
        files2.append(a)
files=files2

file_nm="product_info.xlsx"
with pd.ExcelWriter(file_nm) as writer:
    for file_name in files:
        df = pd.read_excel("practice/참고자료/" + file_name)
        df.to_excel(writer, sheet_name=file_name.replace('.xlsx',''))

 

!pip install python-pptx

 

from pptx import Presentation
prs = Presentation()

 

path="practice/참고자료/"
import os
file_list=os.listdir(path)

file_list_xls=[]
for a in file_list:
    if ".xlsx" in a:
        file_list_xls.append(a)
file_list=file_list_xls
file_list

import pandas as pd

for a in file_list:
    df = pd.read_excel(f"practice/참고자료/{a}")
    print(df)

 

!pip install wordcloud
import sys
from wordcloud import WordCloud

for b in file_list:
    df = pd.read_excel(f"practice/참고자료/{b}")
    df["제목&내용"]=df["제목"]+df["내용"]
    str_unit_sum=''

    for a in range(df.shape[0]):
        str_unit=df.loc[a, "제목&내용"]
        str_unit_sum=str_unit_sum+str_unit
    wc = WordCloud(font_path="practice/BMJUA_ttf.ttf")
    wc.generate(str(str_unit_sum))
    product_name=b.replace(".xlsx", "")
    wc.to_file(f'practice/{product_name}_wordcloud_1.png')

 

from pptx import Presentation

from openpyxl import load_workbook
wb = load_workbook('product_info.xlsx')

 

sheet_names=wb.sheetnames

 

print(wb.sheetnames)
sheet_names=wb.sheetnames

 

for sheet in sheet_names:
    prs = Presentation()

    title_slide_layout=prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)

    title=slide.shapes.title
    subtitle=slide.placeholders[1]
    title.text=sheet
    subtitle.text="뉴스기사 제목+내용 텍스트 기반 워드클라우드"

    from pptx import Presentation
    from pptx.util import Inches
    img_path = f'practice/{sheet}_wordcloud_1.png'
    blank_slide_layout=prs.slide_layouts[6]
    slide=prs.slides.add_slide(blank_slide_layout)

    top=Inches(0)
    left=Inches(0)
    height=Inches(10)
    width=Inches(10/1.33)
    pic=slide.shapes.add_picture(img_path, left, top, height, width)
    prs.save(f'practice/{sheet}.pptx')

728x90