0

I'm using GetTextExtentPoint32W to get width of a text in a cell in MS Excel 2010. The cell width is fetched using ActiveCell.Width. These two widths are then compared to determine whether the text fits in the cell or extends out of the cell. Visually, even though the text fits perfectly in the cell, the text width returned by the method is more than the cell width. Also, when I increase the font size the difference between actual text width and that returned by the method increases. Following is a part of the source code used to achieve the result. Please help me solve this error.

    hDC = ctypes.windll.user32.GetDC(self.windowHandle)
    tempBMP = ctypes.windll.gdi32.CreateCompatibleBitmap(hDC, 1, 1)
    hBMP = ctypes.windll.gdi32.SelectObject(hDC, tempBMP)

    iFontSize = self.excelCellObject.Font.Size
    deviceCaps = ctypes.windll.gdi32.GetDeviceCaps(hDC, 90) 
    iFontSize = int(iFontSize)
    iFontSize = ctypes.c_int(iFontSize)
    iFontSize = ctypes.windll.kernel32.MulDiv(iFontSize, deviceCaps, 72)
    iFontSize = iFontSize * -1
    iFontWeight = 700 if self.excelCellObject.Font.Bold else 400

    sFontName = self.excelCellObject.Font.Name
    sFontItalic = self.excelCellObject.Font.Italic
    sFontUnderline = True if self.excelCellObject.Font.Underline else False
    sFontStrikeThrough = self.excelCellObject.Font.Strikethrough

    #Create a font object with the correct size, weight and style
    hFont = ctypes.windll.gdi32.CreateFontW(iFontSize, 
                                            0, 0, 0, 
                                            iFontWeight, 
                                            sFontItalic, 
                                            sFontUnderline, 
                                            sFontStrikeThrough, 
                                            False, False, False, 
                                            False, False,
                                            sFontName)

    #Load the font into the device context, storing the original font object
    hOldFont = ctypes.windll.gdi32.SelectObject(hDC, hFont)
    sText = self.excelCellObject.Text
    log.io("\nText \t"+sText+"\n")
    textLength = len(sText)

    class structText(ctypes.Structure):
        _fields_ = [("width", ctypes.c_int), 
                    ("height",ctypes.c_int)]

    StructText = structText()
    getTextExtentPoint = ctypes.windll.gdi32.GetTextExtentPoint32W
    getTextExtentPoint.argtypes = [ctypes.c_void_p, 
                                   ctypes.c_char_p, 
                                   ctypes.c_int, 
                                   ctypes.POINTER(structText)]
    getTextExtentPoint.restype = ctypes.c_int

    #Get the text dimensions
    a = ctypes.windll.gdi32.GetTextExtentPoint32W(hDC, 
                                                  sText, 
                                                  textLength,
                                                  ctypes.byref(StructText))

    #Delete the font object we created
    a = ctypes.windll.gdi32.DeleteObject(hFont)
    a = ctypes.windll.gdi32.DeleteObject(tempBMP)

    #Release the device context
    a = ctypes.windll.user32.ReleaseDC(self.windowHandle, hDC)
    textWidth = StructText.width
    cellWidth = self.excelCellObject.Width

Thanks.

Community
  • 1
  • 1
  • Either use `GetTextExtentPoint32A` with an ANSI string, or decode to `unicode` to use `GetTextExtentPoint32W`. For the latter, use `c_wchar_p` in `argtypes`. – Eryk Sun Dec 20 '14 at 17:48
  • Thanks @eryksun for making this post readable. I made the changes suggested by you. It brought me closer to the final solution. I will soon post it. – Siddhartha Gupta Dec 29 '14 at 12:01

1 Answers1

0

I do not use Python or Excel 2010 so cannot comment on your current approach. However, I have struggled with a similar problem. I hope the following points will be helpful.


Background

If you hover over the right boundary of an Excel column and hold the left mouse button you will get a display of the format: “Width: n.nn (mm pixels)”.

The help for the ColumnWidth property says:

One unit of column width is equal to the width of one character in the Normal style. For proportional fonts, the width of the character 0 (zero) is used.

Use the Width property to return the width of a column in points.

As far as I can tell, “Normal style” means the standard font name and size at the time the workbook was created. Changing the standard font name and size for an existing workbook does not appear to have any effect. Changing the font name and size for a worksheet has no effect.

Two example displays for a standard width column are:

For Arial 10         Width: 8.43 (64 pixels)
For Tahoma 10.5      Width: 8.38 (72 pixels)

I have created a string of zeros and attempted to measure how many are visible depending on the width of the column. I found the number of zeroes that I could see matched the value displayed reasonably well for such as subjective measure.

With VBA, the ColumnWidth property of a column or cell sets or returns the width in characters.

With VBA, The read only Width property of a column or cell returns .75 * the width in pixels.

The significance of the above information is that the width values obtained from Excel are not necessarily correct for the font being used.


My problem and the solution I discovered

The problem I had was that I was merging cells and filling them with text. Although Excel will adjust the height of a row so the text within an unmerged cell is visible, it will not do so for a merged cell. I tried many techniques including Microsoft’s .Net, text rendering routines without success. Nothing I tried would reliably emulate Excel’s system for determining the width of text.

The technique I eventually used successfully involved picking a cell to the right and below all used cells for experimental use. I adjusted the width of the experimental cell’s column to match the combined width of the merged cell and copied the formatted value from the cell whose height I wanted. Because the experimental cell was unmerged, Excel would adjust the height as appropriate. I then made sure the source row was at least this height.

The key feature of this technique was that I was not trying to emulate Excel’s system for determining the width of text; I was using Excel’s system.

You would need to try different column widths for the experimental cell. I would start with the current width of the source column. If the row height matches that for a single row, the width of the source column is already sufficient. If the row height is more than that for a single row, I would increase the column width until the row height matched that for a single row and then adjust the source column’s width to match. If you start with the widest value (in characters or Python’s text width) you will probably get the source column width correct at the first attempt which would avoid the need for later adjustments.

Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • My solution worked perfectly for your 2 test cases (if your screen in 2014 was was 96dpi :-) https://stackoverflow.com/a/61041831/1119602 – Winand Apr 05 '20 at 20:53
  • @Winand. Thanks for the link. I am busy with other things at the moment but I will study your code when I have some time. – Tony Dallimore Apr 06 '20 at 08:55