我想使用 python-pptx 模块来更改给定 powerpoint 演示文稿中每个包含文本的形状的校对语言。不幸的是我没有做到。:(
我正在使用 Python 3.6.3 和 python-pptx 0.6.7。
我的代码如下所示:
from pptx import Presentation
from pptx.enum.lang import MSO_LANGUAGE_ID
# In this example code, all proofing language is set to ENGLISH_UK
# all languages can be found in the docs for python-pptx
new_language = MSO_LANGUAGE_ID.ENGLISH_UK
input_file = 'test_pptx.pptx'
output_file = input_file[:-5] + '_modified.pptx'
# Open the presentation
prs = Presentation(input_file)
# iterate through all slides
for slide_no, slide in enumerate(prs.slides):
# iterate through all shapes/objects on one slide
for …Run Code Online (Sandbox Code Playgroud) 我需要使用 python-pptx 生成一个包含线图和条形图的图表。根据文件,据称
\n\n\n\n\n“大多数图表只有一个图,而 python-pptx 还不支持创建多图图表,但您可以在已有的图表上访问多个图。”
\n
http://python-pptx.readthedocs.io/en/latest/user/charts.html
\n\n它仍然成立吗?\n有解决方法吗?\n我最喜欢使用模板中的图表占位符自动创建图表,而不是在模板中预先创建图表,然后只更新值。第一种方法与我目前拥有的所有其他 PowerPoint 自动化更加一致。
\n我需要将图像添加到pptx幻灯片中,并希望将其定位在幻灯片的中心,而无需手动计算尺寸和对齐方式,
我发现了一个关于文本处理的问题: 关于文本居中对齐的问题
以及与文本一起使用的文档: 有关居中对齐文本的文档
但是找不到找到图像的方法,
有想法吗?
我已经开始整理一些代码来获取Pandas数据并将其放入PowerPoint幻灯片中。我使用的模板默认设置为Medium Style 2-Accent 1,因为更改字体和背景非常简单,但还不错,但python-pptx似乎没有实现的部分,它允许更改单元格边框。以下是我的代码,可使用任何解决方案。(更改XML或更改模板默认值以填充更好的样式对我来说是不错的选择,但还没有找到有关如何执行操作的有效文档)。中号样式4对我来说是理想的选择,因为它恰好具有我要寻找的边框。
import pandas
import numpy
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
#Template Location
tmplLoc = 'C:/Desktop/'
#Read in Template
prs = Presentation(tmplLoc+'Template.pptx')
#Import data as Pandas Dataframe - dummy data for now
df = pandas.DataFrame(numpy.random.randn(10,10),columns=list('ABCDEFGHIJ'))
#Determine Table Header
header = list(df.columns.values)
#Determine rows and columns
in_rows = df.shape[0]
in_cols = df.shape[1]
#Insert table from C1 template
slide_layout = prs.slide_layouts[11]
slide = prs.slides.add_slide(slide_layout)
#Set slide title
title_placeholder = slide.shapes.title
title_placeholder.text = …Run Code Online (Sandbox Code Playgroud) 我正在为由 Python-PPTX 生成的幻灯片制作自定义目录。对于已经设置好的幻灯片、页脚和幻灯片编号,我已经可以在生成的 PPTX 中查看它。我相信幻灯片编号已经存在,只需要阅读即可。
我的问题是,是否可以从幻灯片对象或其他地方访问幻灯片编号信息?整体脚本如下所示:
from pptx import Presentation
prs = Presentation()
for x in 'abc':
s = prs.slides.add_slide(prs.slide_layouts[6])
if x=='b':
s.placeholders[0].text = <str: page number of current slide 's'>
Run Code Online (Sandbox Code Playgroud) 我有 PowerPoint 文件,其中包含指向 Excel 文档中不同工作表的数十个链接。我需要以编程方式更改这些链接指向的 Excel 文档。
我很确定我可以使用 VBA 来完成此操作,但由于我无论如何都在 python 中生成 Excel 文档,所以我也更愿意更新那里的链接。
我深入研究了测试.pptx文件的底层 XML 文件,发现链接引用位于该ppt/slides/_rels/文件夹中(解压缩.pptx文件后)
例如,slide1.xml.rels包含多个关系,其中一个具有TargetMode="External"和Target="FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14"
使用该python-ppt包我发现这个相同的引用位于slide.part.rels
例如:
for rel in slides[0].part.rels.values():
if rel.is_external:
print(rel.target_ref)
Run Code Online (Sandbox Code Playgroud)
找到链接的相同路径(即"FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14")
我不知道如何更改这个值(如果可以更改的话)。只是尝试使用它来设置python-pptx它会产生一个AttributeError
有没有办法使用 来修改 PowerPoint 文件的底层 XML python-pptx?或者一些替代策略也可以。
以下代码行展示了如何使用 python 在 PowerPoint 幻灯片中绘制一条线。然而,下面列出的以英寸为单位的四个参数仅取正值,因此无法向后绘制。还有其他方法可以在 PowerPoint 幻灯片中画线吗?
from pptx.enum.shapes import MSO_SHAPE
line1 = slide.shapes.add_shape(MSO_SHAPE.LINE_INVERSE, Inches(6), Inches(6), Inches(1), Inches(2))
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 PPT 从 PPT 中提取表格python-pptx,但是,我不确定如何使用shape.table.
from pptx import Presentation
prs = Presentation(path_to_presentation)
# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_table:
tbl = shape.table
rows = tbl.rows.count
cols = tbl.columns.count
Run Code Online (Sandbox Code Playgroud)
我在这里找到了一篇文章,但接受的解决方案不起作用,给出count属性不可用的错误。
如何修改上面的代码以便在数据框中获取表格?
编辑
请参阅下面的幻灯片图片
我正在尝试使用 python-pptx 从模板插入一张调整大小以适合图片占位符尺寸的图片。从我在文档中找到的内容来看,我不认为 API 可以直接访问此内容。有什么建议我可以使用图书馆或其他方式来做到这一点吗?
我有一个正在运行的代码,它将插入一系列图像到一组模板幻灯片中,以使用 Powerpoint 自动创建报告。
这是执行大部分相关工作的函数。应用程序的其他部分正在创建演示文稿并插入幻灯片等。
def insert_images(slide, slide_num, images_path, image_df):
"""
Insert images into a slide.
:param slide: = slide object from Presentation class
:param slide_num: the template slide number for formatting
:param images_path: the directory to the folder with all the images
:param image_df: Pandas data frame regarding information of each image in images_path
:return: None
"""
placeholders = get_image_placeholders(slide)
#print(placeholders)
image_pool = image_df[image_df['slide_num'] == slide_num]
try:
assert len(placeholders) == len(image_pool.index)
except AssertionError:
print('Length of …Run Code Online (Sandbox Code Playgroud) 我想使用 python-pptx 库从演示文稿中的图表中读取数据。我已经看过替换图表数据的文档,但我不知道如何读取数据。