首页 > 解决方案 > 在 .insert_picture() 之后访问 python-pptx 图片占位符时出错

问题描述

我使用 python-pptx 将图片添加到图片占位符中。然后,我想移动占位符,但是当我使用诸如 placeholder.left 之类的东西时,它应该返回占位符左端的位置,我得到了错误。当我在添加图片之前这样做时,它工作正常......)这里是:

print("pic placeholder left - before inserting picture\n"+str(pic_placeholder.left))
print("\nListing plaseholders - before inserting picture")
for shape in ppt_slide.placeholders:
    print('%d----%s' % (shape.placeholder_format.idx, shape.name))
pic_placeholder = ppt_slide.placeholders[1]
picture = pic_placeholder.insert_picture(s.figures[0].file_name)
print("\nListing plaseholders - after inserting picture")
for shape in ppt_slide.placeholders:
    print('%d----%s' % (shape.placeholder_format.idx, shape.name))
print("pic placeholder left - after inserting 
picture\n"+str(pic_placeholder.left))

这是输出(仔细查看它停止的位置):

2020-03-29 08:18:33 Listing plaseholders - before inserting picture
2020-03-29 08:18:33 
2020-03-29 08:18:33 0----Title 1
2020-03-29 08:18:33 1----Picture Placeholder 2
2020-03-29 08:18:33 2----Text Placeholder 3
2020-03-29 08:18:33 Listing plaseholders - after inserting picture
2020-03-29 08:18:33 
2020-03-29 08:18:33 0----Title 1
2020-03-29 08:18:33 1----Picture Placeholder 2
2020-03-29 08:18:33 2----Text Placeholder 3

由于您将在错误日志中看到的错误,它在这里崩溃:

2020-03-29 08:18:33,171: AttributeError: 'NoneType' object has no
attribute 'x' 2020-03-29 08:18:33,171:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 2463, in __call__ 2020-03-29 08:18:33,171:     return
self.wsgi_app(environ, start_response) 2020-03-29 08:18:33,171: 
2020-03-29 08:18:33,171:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 2449, in wsgi_app 2020-03-29 08:18:33,171:     response =
self.handle_exception(e) 2020-03-29 08:18:33,172:  2020-03-29
08:18:33,172:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 1866, in handle_exception 2020-03-29 08:18:33,172:    
reraise(exc_type, exc_value, tb) 2020-03-29 08:18:33,172:  2020-03-29
08:18:33,172:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/_compat.py",
line 39, in reraise 2020-03-29 08:18:33,172:     raise value
2020-03-29 08:18:33,172:  2020-03-29 08:18:33,172:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 2446, in wsgi_app 2020-03-29 08:18:33,172:     response =
self.full_dispatch_request() 2020-03-29 08:18:33,172:  2020-03-29
08:18:33,172:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 1951, in full_dispatch_request 2020-03-29 08:18:33,172:     rv =
self.handle_user_exception(e) 2020-03-29 08:18:33,173:  2020-03-29
08:18:33,173:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 1820, in handle_user_exception 2020-03-29 08:18:33,173:  
reraise(exc_type, exc_value, tb) 2020-03-29 08:18:33,173:  2020-03-29
08:18:33,173:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/_compat.py",
line 39, in reraise 2020-03-29 08:18:33,173:     raise value
2020-03-29 08:18:33,173:  2020-03-29 08:18:33,173:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 1949, in full_dispatch_request 2020-03-29 08:18:33,173:     rv =
self.dispatch_request() 2020-03-29 08:18:33,173:  2020-03-29
08:18:33,173:   File
"/home/karrot/.virtualenvs/my-virtualenv/lib/python3.5/site-packages/flask/app.py",
line 1935, in dispatch_request 2020-03-29 08:18:33,173:     return
self.view_functions[rule.endpoint](**req.view_args) 2020-03-29
08:18:33,173:  2020-03-29 08:18:33,174:   File
"/home/karrot/mysite/flask_app.py", line 26, in index 2020-03-29
08:18:33,174:     rpg.PG03m(str(pippo[0])) 2020-03-29 08:18:33,174: 
2020-03-29 08:18:33,174:   File
"/home/karrot/mysite/PresentationGenerator09min_method.py", line 324,
in PG03m 2020-03-29 08:18:33,174:     temp_ppt=P1.make_ppt()
2020-03-29 08:18:33,174:  2020-03-29 08:18:33,174:   File
"/home/karrot/mysite/PresentationGenerator09min_method.py", line 229,
in make_ppt

提前感谢并保重!!!电晕就在那里!捷通

标签: pythonpython-pptx

解决方案


将图片插入图片占位符实际上会删除占位符形状并插入图片形状。这也是 PowerPoint 本身的做法;占位符形状是一个<p:sp>元素,而图片是一个<p:pic>元素。

因此,您应该使用占位符预先进行移动,或者使用对新插入的图片形状的引用返回PicturePlaceholder.insert_picture()来操作插入后的新形状。

picture = pic_placeholder.insert_picture(s.figures[0].file_name)
print("picture left - after inserting picture\n%s" % picture.left)

推荐阅读