Open PaleNeutron opened 2 years ago
@PaleNeutron - I am new to this package and trying to do some replacements of keywords and came across your function. Can I know how to call your replace_text function? Is there any example that you can show?
I came here in search of a solution for a issue I am facing while replacing text given below
@SSMK-wq , I have write test for my code which may inspire you:
def test_replace_picture() -> None:
# open pptx file
prs = Presentation("tests/pptx/test_template.pptx")
# replace all in ppt
replace_text(prs, "{report}", "this is my report words")
# replace in one slide
slide = prs.slides[1]
replace_text(slide, "{title}", "This is a title")
replace_text(slide, "{subtitle1}", "small title")
# generate fig
fig_file = BytesIO()
plt.plot([1, 2, 3, 4])
fig = plt.gcf()
fig.savefig(fig_file, format="png")
fig_file.seek(0)
# replace picture
# replace the first picture in slide 0
replace_picture_in_slide(prs.slides[0], fig_file, auto_reshape=True)
# replace the first picture in slide 1
plt.bar(list("qwertyuioplkjhgfdaszcvbnm"[:20]), range(10, -10, -1))
fig = plt.gcf()
replace_picture_in_slide(prs.slides[1], fig, auto_reshape=False, order="l2r")
# replace the second picture in slide 1 with out auto_reshape
replace_picture_in_slide(
prs.slides[1], fig, pic_number=1, auto_reshape=True, order="l2r"
)
@PaleNeutron - doesn't your "replace text" function replace table headers as well? I would like to replace text which are stored as column headers in a table in ppt.
@SSMK-wq , No, it dosen't. It just loop over all top level shapes, but table headers
are nested in tables, you could read this https://python-pptx.readthedocs.io/en/latest/user/table.html
I think check shape by shape.has_table
and then loop all cells in shape.table
would work.
In my opinion, change text in header do not need "replace text" because you can get the exact position of a cell, you can do it just by:
shape.table.cell(0, 0).text = "some text"
@PaleNeutron - I already use has_table
and cell
option. May I kindly seek your help to have a look at this SO post1 and post2
I guess this will give you a better idea on what is the issue that am facing. Any suggestions from you is really helpful for me to solve this problem.
This issue happens only for certain tables (unfortunately, they are more in number) and not all tables
I think replace text or picture in an pptx template is a common use case and wrote function below and I think this project should provide them.
Please review these code.