How to generate bolded text using python wand library? I cant get it to work.
http://docs.wand-py.org/en/0.4.1/wand/drawing.html - regarding documentation styles that are supported:
'undefined;
'normal'
'italic'
'oblique'
'any'
there is no bold style?
sample usage i wanted to do a footer with a date that I get from contents file:
with Image(width=150, height=25,) as img:
draw.font_family = 'MS Reference Sans Serif'
draw.font_size = 14.0
draw.push()
draw.font_style = 'italics'
metrics = draw.get_font_metrics(img, contents['date'], multiline=False)
draw.text(int((img.width - metrics.text_width)/2), int((metrics.text_height)), contents['date'])
draw.pop()
draw(img)
img.save(filename='./temp/footer.png')
Maybe there is some way to make it bold in some quick way? Would really appreciate any help.
"bold" is not a font_style, it's a font_weight: http://docs.wand-py.org/en/0.4.1/wand/drawing.html#wand.drawing.Drawing.font_weight
And the integer values it takes are the values fonts typically use, where 400 is "normal" and 700 is "bold". (These are also used in CSS. See e.g. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Common_weight_name_mapping.)
Although mercator offered a correct answer to the title question, this answer focusing on the nested question:
Maybe there is some way to make it bold in some quick way?
Try using the bold variant typeface directly.
Either specific the bold-font file provided by Microsoft ...
draw.font = 'refsanb.ttf`
... or use the font style with a "Bold" suffix.
draw.font_family = 'MS Reference Sans Serif Bold'
Related
Well as the title already tells, I want to simply format a text without using QFont(). At the moment I'm using it like that:
font = QFont()
font.setBold(True)
label = QLabel()
label.setFont(font)
label.setText("Hello World!")
So far so good. But if I want to have a certain part in a text of a label bold, it gets quite annoying, because I have to create an extra QLabel and use setBold() and put this part into the right position. Is there a way (e.g. markdown) to bold a certain part of a text of a label?
Like that:
label = QLabel()
label.setText("**Hello** World!")
Qt uses a subset of HTML for rich text. This is also the default setting. Try:
label.setText("<b>Hello</b> World!")
The label text format is controlled by the textFormat property. The default is Auto, for possible values see https://doc.qt.io/qt-5/qt.html#TextFormat-enum.
If you use a recent version of Qt (at least 5.14) you can also use Markdown as you suggested:
label.setTextFormat(Qt.MarkdownText)
label.setText("**Hello** World!")
Reference: https://doc.qt.io/qt-5/richtext-html-subset.html
Essentially what I need to do is write a program that takes in many .docx files and puts them all in one, ordered in a certain way. I have importing working via:
import docx, os, glob
finaldocname = 'Midterm-All-Questions.docx'
finaldoc=docx.Document()
docstoworkon = glob.glob('*.docx')
if finaldocname in docstoworkon:
docstoworkon.remove(finaldocname) #dont process final doc if it exists
for f in docstoworkon:
doc=docx.Document(f)
fullText=[]
for para in doc.paragraphs:
fullText.append(para.text) #generates a long text list
# finaldoc.styles = doc.styles
for l in fullText:
# if l=='u\'\\n\'':
if '#' in l:
print('We got here!')
if '#1 ' not in l: #check last two characters to see if this is the first question
finaldoc.add_section() #only add a page break between questions
finaldoc.add_paragraph(l)
# finaldoc.add_page_break
# finaldoc.add_page_break
finaldoc.save(finaldocname)
But I need to preserve text styles, like font colors, sizes, italics, etc., and they aren't in this method since it just gets the raw text and dumps it. I can't find anything on the python-docx documentation about preserving text styles or importing in something other than raw text. Does anyone know how to go about this?
Styles are a bit difficult to work with in python-docx but it can be done.
See this explanation first to understand some of the problems with styles and Word.
The Long Way
When you read in a file as a Document() it will bring in all of the paragraphs and within each of these are the runs. These runs are chunks of text with the same style attached to them.
You can find out how many paragraphs or runs there are by doing len() on the object or you can iterate through them like you did in your example with paragraphs.
You can inspect the style of any given paragraph but runs may have different styles than the paragraph as a whole, so I would skip to the run itself and inspect the style there using paragraphs[0].runs[0].style which will give you a style object. You can inspect the font object beyond that which will tell you a number of attributes like size, italic, bold, etc.
Now to the long solution:
You first should create a new blank paragraph, then you should go and add_run() one by one with your text from your original. For each of these you can define a style attribute but it would have to be a named style as described in the first link. You cannot apply a stlye object directly as it won't copy the attributes over. But there is a way around that: check the attributes that you care about copying to the output and then ensure your new run applies the same attributes.
doc_out = docx.Document()
for para in doc.paragraphs:
p = doc_out.add_paragraph()
for run in para.runs:
r = p.add_run(run.text)
if run.bold:
r.bold = True
if run.italic:
r.italic = True
# etc
Obviously this is inefficient and not a great solution, but it will work to ensure you have copied the style appropriately.
Add New Styles
There is a way to add styles by name but because it isn't likely that the Word document you are getting the text and styles from is using named styles (rather than just applying bold, etc. to the words that you want), it is probably going to be a long road to adding a lot of slightly different styles or sometimes even the same ones.
Unfortunately that is the best answer I have for you on how to do this. Working with Word, Outlook, and Excel documents is not great in Python, especially for what you are trying to do.
I hope you can help me trying to combine a paragraph, my style is called "cursiva" and works perfectly also I have other's but it's the same if I change cursiva to other one. the issue is that If I use this coude o get this.
As you can see guys it shows with a line break and I need it shows togetter.
The problem is that i need to make it like this (one, one) togetter because I need to use two styles, the issue here is that I'm using arial narrrow so if I use italic or bold I need to use each one by separate because the typography does not alow me to use "< i >italic text< /i > ", so I need to use two different styles that actually works fine by separate.
how can I achive this?
cursiva = ParagraphStyle('cursiva')
cursiva.fontSize = 8
cursiva.fontName= "Arialni"
incertidumbre=[]
incertidumbre.extend([Paragraph("one", cursiva), Paragraph("one", cursiva)])
Thank you guys
The question you are asking is actually caused by a workaround for a different problem, namely that you don't know how to register font families in Reportlab. Because that is what is needed to make <i> and <b> work.
So you probably already managed to add a custom font, so the first part should look familiar, the final line is probably the missing link. It is registering the combination of these fonts a family.
from reportlab.pdfbase.pdfmetrics import registerFontFamily
pdfmetrics.registerFont(TTFont('Arialn', 'Arialn.ttf'))
pdfmetrics.registerFont(TTFont('Arialnb', 'Arialnb.ttf'))
pdfmetrics.registerFont(TTFont('Arialni', 'Arialni.ttf'))
pdfmetrics.registerFont(TTFont('Arialnbi', 'Arialnbi.ttf'))
registerFontFamily('Arialn',normal='Arialn',bold='Arialnb',italic='Arialni',boldItalic='Arialnbi')
Is the following function correct, this code is meant to add a phrase to image. Note that i cannot use image.text function or any other but can only use getpixel, putpixel, load, and save.
def insertTxtImage(srcImage, phrase):
pixel = srcImage.getpixel(30,30);
srcImage.putpixel(pixel,phrase);
srcImage.save;
pass
Yes it is homework which can only use getpixel, putpixel, load, and save to insert a phrase in to the image.
I tried to do this with this code but it is giving system error (argument is not a tuple)
def insertTxtImage(srcImage, phrase):
pix = srcImage.load()
pix[0,0] = phrase
srcImage.save()
pass
Thanks for the comments.
No, the functions you are using modify pixels.
To draw font you want to use something like following:
f= pygame.font.Font(None, 12)
surf= f.render(phrase)
srcImage.blit(surf, (30,30))
for more documentation see here: (scroll down a bit)
http://www.pygame.org/docs/ref/font.html
EDIT: nvm, I don't even know what you're doing or trying to do
Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:
lbl.set_markup("<span font_desc='Tahoma 5.4'>%s</span>" % text)
This 1) requires me to set the font , 2) seems like a lot of overhead (having to parse the markup), and 3) would make it annoying to change the font size of buttons and such. Is there a better way?
If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums link which has the following snippet of the the gtkrc file):
style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"
(replace the font with the one you/user need)
Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.
YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.
In C, you can do:
gtk_widget_modify_font(lbl, pango_font_description_from_string("Tahoma 5.4"));
In PyGTK, I believe it's something like:
pangoFont = pango.FontDescription("Tahoma 5.4")
lbl.modify_font(pangoFont)