-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Enable copying of a run's font #1308
Comments
I should mention that I tested my changes in #1309 to be working. It allowed me to recover the font information and pass it to the new run generated via Paragraph::text . |
@luissantosHCIT if you work at the You'll need to search around for examples, but some of the things you'll need to know about are the So something roughly like: original_rPr = run._r.get_or_add_rPr()
new_rPr = copy.deepcopy(original_rPr)
new_r = para.add_run()._r
new_r.replace(new_r.get_or_add_rPr(), new_rPr) |
Hi @scanny. Thank you for taking the time to share the snippet. Yes, I have used lxml for a while now. I was trying to avoid the lower level api. It makes more sense to me to abstract away the private member access with getters and setters. Do you think that perhaps it would be better to have a property like
What do you think? |
Updated my branch. I only needed to add the following.
@scanny Let me know if it looks good or if you would like me to do something else. |
Hello,
I am new to this project and have been enjoying the work done.
For my own project, I replace some text fields with specific values.
Unfortunately, I noticed that the formatting gets lost when using the Paragraph::text property.
For example,
becomes
Inspection of the XML output revealed that the run's embedded font information gets lost.
Finally, I found this comment in the source code for the Paragraph class
Looking deeper, I see why the call to clear() was needed.
In my testing, I can see how Word can sometimes split text over multiple runs.
As a result, I resorted to logic like so in my program in order to recover the font information.
The one issue is that new_run.font = old_run.font was returning an error.
I went ahead and added the missing font.setter and color.setter to the respective properties in run.py and font.py.
I am going to open a pull request to associate with this issue.
Ideally, I can retain the formatting of one of the runs when changing the text via an instance of Paragraph. Since I understand this is tricky and probably not worth the time, I think enabling the ability to copy the font formatting from a run to another would be helpful for those who are assuming 1 formatting to one block of text (meaning, even if Word splits the text into multiple runs, all runs have the same formatting). I think this is a good compromise for me at this time.
Let me know what you think, how I can be of assistance, or how I can adjust my solution in #1309 to better match expectations for this project.
Thank you for your excellent work!
The text was updated successfully, but these errors were encountered: