Excel에 첨부 된 이미지를 자르고 Power Point에 붙이는 방법
파이썬 패키지 준비
파이썬에서 Windows Office를 조작하기위한 패키지에는 win32com이라는 것이 있습니다. win32com을 사용하면 VBA에서 Excel 및 Power Point를 조작하는 것과 유사한 코드로 Excel 및 Power Point를 조작 할 수 있습니다. 그래서 준비를 위해 win32com을 pip으로 설치하십시오.
pip install pywin32
이미지를 붙인 Excel 파일
이미지를 삽입하여 Excel에서 쉽게 볼 수 있도록 정리합니다.
Excel에 이미지를 삽입하면 VBA에서 Workbooks(1).WorkSheets(1).Shapes(number)로 가져올 수 있습니다.
이미지를 삽입한 순서대로 숫자(number)가 붙습니다.
python 코드
win32com을 사용하여 Excel 이미지를 파워 포인트에 붙입니다.
파이썬으로 실행할 때 아래 코드를 그대로 실행하면 original.xlsx 이미지를 paste.pptx에 붙입니다.
그러나 클립보드를 사용하여 복사하기 때문에 실행 중에는 ctr+C 복사본을 사용할 수 없게 될 것입니다.
JUPYTER NOTEBOOK을 사용하는 경우 close_excel_by_force 및 close_ppt_by_force의 주석 처리를 해제하고 코드를 사용하도록 설정하면 Excel과 Power Point가 시작된 상태로 유지됩니다. 프로세스를 직접 종료해야 합니다.
import win32com.client
import os
def close_excel_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
hwnd = app.Hwnd
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
def close_ppt_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
import psutil
process = [proc for proc in psutil.process_iter() if proc.name() == "POWERPNT.EXE"]
p = process[0].pid
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
directory = os.path.dirname(os.path.abspath(__file__))
excelfile = os.path.join(directory, "original.xlsx")
pptfile = os.path.join(directory,"paste.pptx")
try:
#start power point and excel
PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")
#make a powerpoint file and an excel file
presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename=excelfile,ReadOnly=1,UpdateLinks=False)
count =0
for ws in workbook.Worksheets:
for chart in ws.Shapes:
# Trim a picture
pf = chart.PictureFormat
pf.CropLeft = 20
pf.CropTop = 20
#copy picture to clipboard
chart.CopyPicture()
#Add a new blank slide to Power Point
Slide=presentation.Slides.Add(presentation.Slides.Count+1,12)
#paste the picture to the slide
p = Slide.Shapes.Paste()
p.Left=100
p.Top=100
#Add a text
textBox = Slide.Shapes.AddTextbox(Orientation=1,
Left=100,Top=10,Width=200,Height=50)
textBox.TextFrame.TextRange.Text = "Test Box"
print(count)
count = count+1
except Exception as e:
print("error")
print(str(e))
finally:
#save power point file
presentation.SaveAs(pptfile)
presentation.Saved=False
presentation.Close()
#close the excel file.
#If "Saved" option is True, the workbook will be closed forcely without checking update.
#It is becauce it is regarded that the file has already been saved.
workbook.Saved=True
workbook.Close()
print("Finished Copying picutes from Excel to Powerpoint Presentation")
#quit excel
Excel.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_excel_by_force(Excel)
print("Quit Excel")
#quit excel
PowerPoint.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_ppt_by_force(PowerPoint)
print("Quit Power Point")
결과
위의 코드를 실행하면 Excel의 이미지가 트림되어 Power Point에 붙어 다음과 같이됩니다.
Reference
이 문제에 관하여(Excel에 첨부 된 이미지를 자르고 Power Point에 붙이는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Toyoharu-Nishikawa/items/6d195ab78a99cfe46663
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
pip install pywin32
이미지를 삽입하여 Excel에서 쉽게 볼 수 있도록 정리합니다.
Excel에 이미지를 삽입하면 VBA에서 Workbooks(1).WorkSheets(1).Shapes(number)로 가져올 수 있습니다.
이미지를 삽입한 순서대로 숫자(number)가 붙습니다.
python 코드
win32com을 사용하여 Excel 이미지를 파워 포인트에 붙입니다.
파이썬으로 실행할 때 아래 코드를 그대로 실행하면 original.xlsx 이미지를 paste.pptx에 붙입니다.
그러나 클립보드를 사용하여 복사하기 때문에 실행 중에는 ctr+C 복사본을 사용할 수 없게 될 것입니다.
JUPYTER NOTEBOOK을 사용하는 경우 close_excel_by_force 및 close_ppt_by_force의 주석 처리를 해제하고 코드를 사용하도록 설정하면 Excel과 Power Point가 시작된 상태로 유지됩니다. 프로세스를 직접 종료해야 합니다.
import win32com.client
import os
def close_excel_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
hwnd = app.Hwnd
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
def close_ppt_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
import psutil
process = [proc for proc in psutil.process_iter() if proc.name() == "POWERPNT.EXE"]
p = process[0].pid
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
directory = os.path.dirname(os.path.abspath(__file__))
excelfile = os.path.join(directory, "original.xlsx")
pptfile = os.path.join(directory,"paste.pptx")
try:
#start power point and excel
PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")
#make a powerpoint file and an excel file
presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename=excelfile,ReadOnly=1,UpdateLinks=False)
count =0
for ws in workbook.Worksheets:
for chart in ws.Shapes:
# Trim a picture
pf = chart.PictureFormat
pf.CropLeft = 20
pf.CropTop = 20
#copy picture to clipboard
chart.CopyPicture()
#Add a new blank slide to Power Point
Slide=presentation.Slides.Add(presentation.Slides.Count+1,12)
#paste the picture to the slide
p = Slide.Shapes.Paste()
p.Left=100
p.Top=100
#Add a text
textBox = Slide.Shapes.AddTextbox(Orientation=1,
Left=100,Top=10,Width=200,Height=50)
textBox.TextFrame.TextRange.Text = "Test Box"
print(count)
count = count+1
except Exception as e:
print("error")
print(str(e))
finally:
#save power point file
presentation.SaveAs(pptfile)
presentation.Saved=False
presentation.Close()
#close the excel file.
#If "Saved" option is True, the workbook will be closed forcely without checking update.
#It is becauce it is regarded that the file has already been saved.
workbook.Saved=True
workbook.Close()
print("Finished Copying picutes from Excel to Powerpoint Presentation")
#quit excel
Excel.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_excel_by_force(Excel)
print("Quit Excel")
#quit excel
PowerPoint.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_ppt_by_force(PowerPoint)
print("Quit Power Point")
결과
위의 코드를 실행하면 Excel의 이미지가 트림되어 Power Point에 붙어 다음과 같이됩니다.
Reference
이 문제에 관하여(Excel에 첨부 된 이미지를 자르고 Power Point에 붙이는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Toyoharu-Nishikawa/items/6d195ab78a99cfe46663
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import win32com.client
import os
def close_excel_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
hwnd = app.Hwnd
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
def close_ppt_by_force(app):
import win32process
import win32gui
import win32api
import win32con
import time
# Get the window's process id's
import psutil
process = [proc for proc in psutil.process_iter() if proc.name() == "POWERPNT.EXE"]
p = process[0].pid
# Allow some time for app to close
time.sleep(10)
# If the application didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
except:
pass
directory = os.path.dirname(os.path.abspath(__file__))
excelfile = os.path.join(directory, "original.xlsx")
pptfile = os.path.join(directory,"paste.pptx")
try:
#start power point and excel
PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")
#make a powerpoint file and an excel file
presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename=excelfile,ReadOnly=1,UpdateLinks=False)
count =0
for ws in workbook.Worksheets:
for chart in ws.Shapes:
# Trim a picture
pf = chart.PictureFormat
pf.CropLeft = 20
pf.CropTop = 20
#copy picture to clipboard
chart.CopyPicture()
#Add a new blank slide to Power Point
Slide=presentation.Slides.Add(presentation.Slides.Count+1,12)
#paste the picture to the slide
p = Slide.Shapes.Paste()
p.Left=100
p.Top=100
#Add a text
textBox = Slide.Shapes.AddTextbox(Orientation=1,
Left=100,Top=10,Width=200,Height=50)
textBox.TextFrame.TextRange.Text = "Test Box"
print(count)
count = count+1
except Exception as e:
print("error")
print(str(e))
finally:
#save power point file
presentation.SaveAs(pptfile)
presentation.Saved=False
presentation.Close()
#close the excel file.
#If "Saved" option is True, the workbook will be closed forcely without checking update.
#It is becauce it is regarded that the file has already been saved.
workbook.Saved=True
workbook.Close()
print("Finished Copying picutes from Excel to Powerpoint Presentation")
#quit excel
Excel.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_excel_by_force(Excel)
print("Quit Excel")
#quit excel
PowerPoint.Quit()
#<caution> when you run this script on jupyter notebook, uncomment the following script line.
#close_ppt_by_force(PowerPoint)
print("Quit Power Point")
위의 코드를 실행하면 Excel의 이미지가 트림되어 Power Point에 붙어 다음과 같이됩니다.
Reference
이 문제에 관하여(Excel에 첨부 된 이미지를 자르고 Power Point에 붙이는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Toyoharu-Nishikawa/items/6d195ab78a99cfe46663텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)