![]() |
How can I get the width of a string in Python? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I get the width of a string in Python? (/thread-18652.html) Pages:
1
2
|
How can I get the width of a string in Python? - aquerci - May-26-2019 is there a way to get the width of a string in pixel or in inches using Python 3? RE: How can I get the width of a string in Python? - Larz60+ - May-26-2019 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. RE: How can I get the width of a string in Python? - aquerci - May-26-2019 (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. 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. RE: How can I get the width of a string in Python? - heiner55 - May-26-2019 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. RE: How can I get the width of a string in Python? - aquerci - May-26-2019 (May-26-2019, 02:05 PM)heiner55 Wrote: GUI libraries like wxpython can do that: 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)? RE: How can I get the width of a string in Python? - heiner55 - May-26-2019 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() RE: How can I get the width of a string in Python? - aquerci - May-26-2019 (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. RE: How can I get the width of a string in Python? - heiner55 - May-27-2019 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)
RE: How can I get the width of a string in Python? - heiner55 - May-27-2019 If you are familiar with tkinter, there is probably a similar function. PyQt has a similiar function: https://stackoverflow.com/questions/8633433/qt-get-the-pixel-length-of-a-string-in-a-qlabel On Windows wxPython calls this function from win32 api: https://docs.microsoft.com/en-us/windows/desktop/api/Wingdi/nf-wingdi-gettextmetrics You can call it with pywin32 (https://pypi.org/project/pywin32/) RE: How can I get the width of a string in Python? - heiner55 - May-27-2019 Or see here: https://stackoverflow.com/questions/32555015/how-to-get-the-visual-length-of-a-text-string-in-python |