Python Forum
How can I get the width of a string in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get the width of a string in Python?
#11
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)
Reply
#12
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)
Reply
#13
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?
Reply
#14
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)
Reply
#15
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,181 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 422 Jun-10-2023, 03:40 PM
Last Post: pandabay
  width of Unicode character Skaperen 6 2,709 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,411 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  fixed width numbers Skaperen 15 8,593 May-27-2019, 09:42 AM
Last Post: Skaperen
  printing text tables with consistent width Skaperen 7 10,677 Jul-01-2018, 02:34 AM
Last Post: Skaperen
  How to measure an inclined beam width and height in image using python? zyb1003 1 3,224 Nov-07-2017, 05:02 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020