from pptx import Presentation
from pptx.util import Inches
from pptx.util import Length
def insert_chart_ppt(test_items):
prs = Presentation()
# left = 0 # this is x axis
# top = 0 # this is y axis
row_count = 3
column_count = 4
width = prs.slide_width / getattr(Length, "_EMUS_PER_INCH")
height = prs.slide_height / getattr(Length, "_EMUS_PER_INCH")
row_height = (height - 0.5) / row_count
column_width = (width - 0.5) / column_count
# () means generator, if generator is used out, you can't use it again
rows = list(x * row_height for x in range(row_count))
columns = list(y * column_width for y in range(column_count))
img_paths = []
for item in test_items:
img_paths.append("analog_" + item.desc + ".png")
if len(img_paths) == row_count * column_count:
batch_insert(img_paths, prs, rows, columns)
img_paths.clear()
if len(img_paths) > 0:
batch_insert(img_paths, prs, rows, columns)
prs.save('chart_insert.pptx')
def batch_insert(img_paths, prs, rows, columns):
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
pic_width = Inches(3)
pic_height = Inches(2.27)
for row_index, row in enumerate(rows):
for columns_index, column in enumerate(columns):
index = row_index*len(columns) + columns_index
if index < len(img_paths):
img_path = img_paths[index]
print("Insert {} into ppt".format(img_path))
slide.shapes.add_picture(img_path, Inches(column), Inches(row), height=pic_height, width=pic_width)
inset batch picture into ppt
猜你喜欢
转载自blog.csdn.net/lantianjialiang/article/details/91842298
今日推荐
周排行