-
Notifications
You must be signed in to change notification settings - Fork 515
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
Changing Text without editing the formatting #285
Comments
@powahftw Character formatting (font characteristics) are specified at the Although it would not work for all cases one might want, a useful behavior would be to replace the text in a paragraph, retaining the formatting present in the first run. This could be accomplished like this: def replace_paragraph_text_retaining_initial_formatting(paragraph, new_text):
p = paragraph._p # the lxml element containing the `<a:p>` paragraph element
# remove all but the first run
for idx, run in enumerate(paragraph.runs):
if idx == 0:
continue
p.remove(run._r)
paragraph.runs[0].text = new_text
paragraph = textframe.paragraph[0] # or wherever you get the paragraph from
new_text = 'foobar'
replace_paragraph_text_retaining_initial_formatting(paragraph, new_text) I haven't tested this, maybe you can report back any mistakes if you try it out, but I think it gives the gist. This would be roughly how such a feature would be implemented. |
@scanny Just tried your suggested function and it works perfectly - thanks! |
wow... this is perfect. I had written a loop of try/except that stored the attributes of the first run and then re-applied them after changing the text. Feel like a barbarian. |
Hi guys, Thanks @scanny for the snippet! It was really helpful. So, I had to modify your snippet into this
Hope this helps... or well, I just wanted to share the solution to the whole morning of frustration... |
This really helps us.... thank you very much @scanny |
Hi @scanny , Thanks for the snippet! Question though - is changing the text on the paragraph really supposed to clear the formatting or is that a bug? Thanks |
@franz-see The quick, brown fox. with: The lazy yellow dog. How would you do that with something like Character formatting provided by the paragraph-style is preserved, and generally this produces the best possible result. If you need to apply "inline" character formatting, then you need to do it yourself, run-by-run. One way to do this is to assign |
That's really useful, thanks @scanny. Is there a way to keep the formatting when replacing text in tables (cells), too?
from: https://stackoverflow.com/questions/37924808/python-pptx-power-point-find-and-replace-text-ctrl-h |
I think, I solved it. It was just a matter of finding how to access the Thank you for this great module!
|
Same code, slightly refactored for length and indent-level. I think there was a bug in there too, you deleted runs before capturing the text they contain. def iter_table_cells(shapes):
for shape in shapes:
if not shape.has_table:
continue
for row in shape.table.rows:
for cell in row.cells:
yield cell
for cell in iter_table_cells(shapes):
for match, replacement in replacements.items():
for paragraph in cell.text_frame.paragraphs:
if match not in paragraph.text:
continue
orig_text = paragraph.text
# --- the lxml element containing the `<a:p>` paragraph element ---
p = paragraph._p
# --- remove all but the first run ---
for run in paragraph.runs[1:]:
p.remove(run._r)
run = paragraph.runs[0]
run.text = orig_text.replace(str(match), str(replacement)) |
You are very kind. Thanks again. |
I am trying to highlight a specific word in red color in a pptx file using the below function.
While it works, it loses the formatting and also adds some unusual characters like '_x000B' to some words where it finds the highlighted word. Could you please tell me what I am missing? Full code below:
|
To edit some text i currently use pharagraphs and runs and apply all the styles i'm interested back. It would be interesting to have a way to just change the text of an existing text, leaving the formatting untouched.
The text was updated successfully, but these errors were encountered: