Posts: 606
Threads: 3
Joined: Nov 2016
Width with wxPython:
#!/usr/bin/python3
import wx
app = wx.App()
def width_and_height_calculator_in_pixel(txt, fontname, fontsize):
font = wx.Font(wx.FontInfo(fontsize).FaceName(fontname))
dc = wx.ScreenDC()
dc.SetFont(font)
size = dc.GetTextExtent(txt)
return size
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 24) )
print()
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 24) ) Output: (69, 15)
(90, 19)
(153, 32)
(74, 17)
(98, 22)
(159, 33)
Posts: 606
Threads: 3
Joined: Nov 2016
May-27-2019, 03:59 PM
(This post was last modified: May-27-2019, 03:59 PM by heiner55.)
Width with tkinter:
#!/usr/bin/python3
import tkinter
import tkinter.font
app = tkinter.Frame()
def width_and_height_calculator_in_pixel(txt, fontname, fontsize):
font = tkinter.font.Font(family=fontname, size=fontsize)
return (font.measure(txt), font.metrics('linespace'))
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 24) )
print()
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 24) ) Output: (70, 16)
(91, 20)
(155, 34)
(73, 16)
(97, 21)
(168, 35)
Posts: 32
Threads: 16
Joined: Oct 2018
May-27-2019, 04:38 PM
(This post was last modified: May-27-2019, 04:40 PM by aquerci.)
thanks heiner55, the function "width_and_height_calculator_in_pixel" works but, there is an issue.. I'm not sure but probably the width and height values are wrong. see the picture:
[Image: 18d5eb1235200004.jpg]
Excel does not agree with the script result. the script gave me "102" for the "text width", but not Excel! for him the size is bigger. how is it possibile? then I have another question. to set the "width column" in Excel I have to specify a width in inch. following an exaple:
# set the first colum width in 11" (inch):
worksheet.set_column(0, 1, 11) so, it's better to have the "text width" in inch and not in pixel. how can I reach my goal?
Posts: 606
Threads: 3
Joined: Nov 2016
May-27-2019, 04:59 PM
(This post was last modified: May-27-2019, 04:59 PM by heiner55.)
Width with PIL:
Copy calibri.ttf and helvetica.ttf into same directory as python script:
#!/usr/bin/python3
from PIL import Image, ImageDraw, ImageFont
def width_and_height_calculator_in_pixel(txt, fontname, fontsize):
font = ImageFont.truetype(font=fontname.lower() + '.ttf', size=fontsize)
return font.getsize(txt)
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Calibri", 24) )
print()
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 11) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 14) )
print( width_and_height_calculator_in_pixel("Hello World", "Helvetica", 24) ) Output: (55, 9)
(66, 11)
(118, 18)
(55, 9)
(73, 11)
(122, 20)
Posts: 606
Threads: 3
Joined: Nov 2016
Sorry, I don't have Excel.
So I cannot help you.
But I assume that Excel can also calculate in pixel,
inch is only the default.
|