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?
#1
is there a way to get the width of a string in pixel or in inches using Python 3?
Reply
#2
It can be done, but you would have to know the width of each character (given the font, and all font attributes), not trivial.
It would be much easier with fixed width font.
Reply
#3
(May-26-2019, 03:22 AM)Larz60+ Wrote: It can be done, but you would have to know the width of each character (given the font, and all font attributes), not trivial.
It would be much easier with fixed width font.

great! can you give an example please? I'm working with an excel file, so the default font attributes would always be the following:

Font: Calibri
Font size: 11
Format: Bold (in my excel file it regards just the first row)

I wrote a script to get some information from a text file and put them in a new excel file using xlsxwriter module. it works but each column width don't fit the data. following picture shows you an example:
[Image: 8e4e5c1233813174.jpg]
to solve this cosmetic issue, I could get the maximum width of the data written in each column, and then adjusting the column width for each ones of them. but to do this I need to understand how to get the "text" within in pixel or in another kind of unit.
Reply
#4
GUI libraries like wxpython can do that:
GetTextExtent(self, st)
Return the dimensions of the given string’s text extent using the currently selected font.
Reply
#5
(May-26-2019, 02:05 PM)heiner55 Wrote: GUI libraries like wxpython can do that:
GetTextExtent(self, st)
Return the dimensions of the given string’s text extent using the currently selected font.

sorry but I'm not an expert in Python. can you give me a simple example please? I have just install the "wxpython" library via pip but I don't understand how can I use it to reach my goal. for example, what is the "width" of the "hello!" text (font: Calibri, font size: 11)?
Reply
#6
You should have experience with GUI programming or 2D-programming.
If not, it is difficult.

#!/usr/bin/python3
import wx  # wxpython 4.x
app = wx.App()

window = wx.Frame(None, title = "wxPython Frame", size = (300,200))
panel  = wx.Panel(window)

txt   = "Hello World"
label = wx.StaticText(panel, label = txt, pos = (100,50))

dc   = wx.ScreenDC()
dc.SetFont(label.GetFont())
size = dc.GetTextExtent(txt)
txtwh = "width=%d  height=%d" % (size.width, size.height)
label = wx.StaticText(panel, label = txtwh, pos = (100,100))

window.Show(True)
app.MainLoop()
Reply
#7
(May-26-2019, 04:09 PM)heiner55 Wrote: You should have experience with GUI programming or 2D-programming. If not, it is difficult.

yeah, sincerely I don't have much experience with GUI programming. I studied a little bit of Tkinter but, I have never create a final program with it. it was just to understand the concepts, etc.. from my side it's a little bit difficult understand all your code lines.

sorry heiner55, maybe it's a stupid question but, isn't there a function or something else like this?
>>> # "wdth_and_height_calculator_in_pixel" is only an immaginary function. it accepts tree arguments:
>>> # "text", "text font", and "font size":
>>> text_size = wdth_and_height_calculator_in_pixel("Hello World", Calibri, 11)
>>>
>>> text_size
>>> (63, 15) # the result is a tuple with two values (width and height in pixel)
I would very like to reach my goal using few commands and following the simpler way, exactly as in my example.
Reply
#8
You can try this with caution:

#!/usr/bin/python3
import wx
app = wx.App()

def width_and_height_calculator_in_pixel(txt, fontname, fontsize):
    dc   = wx.ScreenDC()
   #dc.SetFont(...) # todo: https://wxpython.org/Phoenix/docs/html/wx.DC.html#wx.DC.SetFont
    size = dc.GetTextExtent(txt)
    return size

text_size = width_and_height_calculator_in_pixel("Hello World", "Calibri", 11)
print(text_size)
Output:
(75, 17)
Reply
#9
If you are familiar with tkinter,
there is probably a similar function.

PyQt has a similiar function:
https://stackoverflow.com/questions/8633...n-a-qlabel

On Windows wxPython calls this function from win32 api:
https://docs.microsoft.com/en-us/windows...extmetrics
You can call it with pywin32 (https://pypi.org/project/pywin32/)
Reply
#10
Or see here:
https://stackoverflow.com/questions/3255...-in-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,159 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 418 Jun-10-2023, 03:40 PM
Last Post: pandabay
  width of Unicode character Skaperen 6 2,703 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,400 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  fixed width numbers Skaperen 15 8,576 May-27-2019, 09:42 AM
Last Post: Skaperen
  printing text tables with consistent width Skaperen 7 10,666 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,222 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