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

how_to_apply_a_style_to_a_paragraph.py simply duplicates paragraphs with new style. #21

Open
Sazu-bit opened this issue Mar 28, 2023 · 4 comments

Comments

@Sazu-bit
Copy link

Sazu-bit commented Mar 28, 2023

for selectedParagraph in body.get_paragraphs():
	
	# Work on the element itself
	selectedParagraph.set_span("Text Body")

so I tried the append route (as described in the recipes) but that just adds an extra element. I hunted down what seems to be the correct way to do it, which is 'set_span' since I'm iterating through the paragraphs but... no joy.

Still stays as "Default Paragraph Style".

What gives?

Additional

For applying a style to a paragraph the recipes offer:

body.append(Paragraph("some text", style="Text Body"))

However, append does exactly what it is designed to do and basically copies the paragraph.

@Sazu-bit Sazu-bit changed the title Set_Span doesn't appear to be doing anything how_to_apply_a_style_to_a_paragraph.py simply duplicates paragraphs with new style. Mar 28, 2023
@jdum
Copy link
Owner

jdum commented Mar 28, 2023

Hi,
About set_span() : The syntax is either Paragraph.set_span("syle_name", regex="some regex to find relevant text target") or Paragraph.set_span("syle_name", offset=x, length=y) if the exact position of the target is known.

And there is some bug: if neither regex or offset are provided, nothing happens (it should be at least raise an error)

@Sazu-bit
Copy link
Author

Sazu-bit commented Mar 28, 2023

I don't need to apply a style to specific words, I need to apply the style to the paragraph. It's just simple clean up work. So the regex is pretty useless to me.

Instead I worked around this by adding:

Paragraph.delete(paragraph) just before a body.append, basically I save the content earlier (as I'm already checking if it's an empty line or not).

Since you're reading this thread though...

Thank you so much for creating this!

@jdum
Copy link
Owner

jdum commented Mar 28, 2023

Maybe Paragraph.set_span("syle_name", offset=0), that should apply to whole content of the paragraph.

@Sazu-bit
Copy link
Author

Sazu-bit commented Mar 28, 2023

No joy.

I've tried "Text_Body", "text_body", "Text Body", "text body" with offset=0 but once the document is generated, there's no change the the paragraphs.

for selectedParagraph in body.get_paragraphs():
		
	# Work on the content inside the paragraph
	paragraphContent = selectedParagraph.text_recursive
	selectedParagraph.set_span("Text_Body", offset=0)

I can't use Paragraph unless I can specify it somewhere. (I love how python doesn't care about key words...).

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

No branches or pull requests

2 participants