-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: Paragraph.delete() #33
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
Comments
Would like to see this available for python-docx. It would be very useful in populating a document full of placeholders given that it would allow the placeholder paragraph to be deleted if the value to populate the placeholder is None. |
You should be able to do this for the simple case with this code: def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None Any subsequent access to the "deleted" The reason it's not in the library yet is because the general case is much trickier, in particular needing to detect and handle the variety of linked items that can be present in a paragraph; things like a picture, a hyperlink, or chart etc. But if you know for sure none of those are present, these few lines should get the job done. |
That works! Thank you!! |
Glad it worked out Jeff :) |
Steve, thanks so much. I was having trouble after merging cells in a table which left extra empty paragraphs. Used your function and worked great, which let the cells shrink back by getting rid of empty space. Used it in a nested loop as follows: delete_paragraph(table.rows[rx].cells[cx].paragraphs[-1]) thanks - wayne (retired HW designer, having fun with python while hopefully helping out the non-profit I volunteer for) |
Hi @scanny |
What's the difference compared to this solution?
|
Well, in fact, on review, there is an error in that code. The last line should be:
But as for the rest of it:
|
Thanks for this @scanny |
I have this same problem. However, when I use the delete_paragraph function with the corrected last line, the resulting document throws an error when opened that reads "Word found unreadable content in document_name.docx. Do you want to recover the contents of this document?" Clicking yes works to open the document, but I'm trying to figure out why deleting the paragraphs is causing this problem. I think it might be related to the fact that this paragraph exists in a merged cell, but it sounds like @waynerth didn't experience this problem. Any thoughts? Thanks for your work on this @scanny! |
@mrufsvold each cell must contain at least one block item, so a paragraph or a table. If you get rid of all the paragraphs, that leaves the cell in an invalid state. You might want to delete |
@scanny That makes complete sense! Thanks for your quick reply. I'll give that a shot when I get back to that project! |
It worked! |
Glad you got it working @mrufsvold :) |
@scanny Does that mean that if I delete a paragraph containing a link, my document will/might crash because the linked stuff is still kept/referenced somewhere else in the document ... or something alike? |
It depends a little on what you mean by link, but deleting is not so much a problem in practice as copying is. If you have a hyperlink, for example, in a paragraph, that hyperlink element in the XML contains a relationship reference (like "rId7") to a If you have something "bigger", like say an image embedded in the paragraph (a so-called inline-shape), and you delete the paragraph without attending to the now-dangling relationship, then both the So deleting a paragraph is worth trying if you don't mind a little wasted space. But if you copy a paragraph and don't re-establish the relationships (which may need to change "name", e.g. "rId7" -> "rId9") and also copy over target part(s) (e.g. the image in the example above) then that will definitely trigger a repair error on loading the document because Word can't find the image to render in that paragraph. |
I think deleting is working for me, at least for the tests I made with many small controlled documents. Now with a big document (where I do lots of things, not just deleting paragraphs) I am getting errors when opening it.
Thanks @scanny |
Wow, thank you. It works!!! |
In order to modify an existing document
As a developer using python-pptx
I need a way to delete a paragraph
Need to account for the possibility the paragraph contains the last reference to a relationship, such as might a hyperlink or inline picture.
The text was updated successfully, but these errors were encountered: