Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: remove single run/paragraph #144

Open
theres opened this issue Jan 22, 2015 · 6 comments
Open

feature: remove single run/paragraph #144

theres opened this issue Jan 22, 2015 · 6 comments
Labels

Comments

@theres
Copy link

theres commented Jan 22, 2015

I make some pptx templates processing and this is really desired feature for me. API proposal:

paragraph.remove(run)

and

textframe.remove(paragraph)
@scanny
Copy link
Owner

scanny commented Jan 22, 2015

Hi @theres, I think the API we'll use is .delete() on each individual object for which it's valid, so something like this:

paragraph.runs[0].delete()
run.delete()
paragraph.delete()
# etc. ...

In the meantime, this code should do the trick for plain content. The trickiness comes in when there are links to external content, like a picture or hyperlink:

def delete_paragraph(paragraph):
    p = paragraph._p
    parent_element = p.getparent()
    parent_element.remove(p)

# this second form is a little more direct, although perhaps not
# quite as clear on all the steps that are happening
def delete_run(run):
    r = run._r
    r.getparent().remove(r)

You'll need to be careful about using any remaining reference to the paragraph after calling delete_paragraph(). It will seem to work but won't affect the presentation content that gets saved. That's one of the reasons using the form delete_paragraph(text_frame.paragraphs[idx]) would be preferred since it doesn't leave a reference hanging around.

Dealing with these smaller details and edge cases will be some of the work that will go into the actual feature when we add it.

Let us know how you go if you decide to use the workaround function :)

@scanny scanny added the text label Jan 22, 2015
@theres
Copy link
Author

theres commented Jan 22, 2015

Thanks!
I found similar workaround by myself and it's working for me now, but it would be great to have some consistent and stable API for delete and other basic operations (ex. insert in specific place new or copy of run/paragraph/br's).

Anyway, great job and thank you again!

@ghost
Copy link

ghost commented Apr 17, 2015

Im stuck on this, prs.slides[slideIdx].shapes[1] is an textbox, how can i get rid from it?
i just can do prs.slides[slideIdx].shapes[1].clear()

@zhouaide0924
Copy link

Hi Scanny,

is it possible that you give a example how to use this function delete_paragraph()? i always got errors when i call it after i open a word file using Document() function, do I need do any other preparation before call this function?

Thanks so much!

@scanny
Copy link
Owner

scanny commented Oct 13, 2019

@zhouaide0924 post a small example that doesn't work and describe the error you're getting in a post on StackOverflow tagged with the "python-docx" tag and I'll take a look.

@zhouaide0924
Copy link

Hi Scanny, Thanks for your response, it is work now. I find I used it in a wrong way. Sorry for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants