Daniel San
Daniel San

Reputation: 101

What is the unit used in OpenPyXL for column width?

The Units documentation didn't answer my question.

Apparently it is based on OOXML measurements, but I'm not sure because the measurement of row height and column width is not the same, and the link above says: "The main unit in OOXML is a twentieth of a point.". So that can't be true. I tried to shoot some numbers and see with LibreOffice Calc but nothing fits:

self.worksheet.column_dimensions['B'].width = 60
self.worksheet.row_dimensions[1].height = 4

I found that the rows are in points, but column width gives:

pt 333.35
pc 27.78 
4.63 "
cm 11.76

Upvotes: 10

Views: 4872

Answers (3)

Demetry Pascal
Demetry Pascal

Reputation: 554

Very strange, I'm trying to solve this problem and got fudges like:


HEIGHT_FUDGE = 72/96 = 3/4 # 96 is my screen dpi, 72 is a standart dpi

# my screen resolution is 1366*768
# 95.25 value was found here https://openpyxl.readthedocs.io/en/stable/_modules/openpyxl/utils/units.html
WIDTH_FUDGE = 768/1366*(1 - 72/95.25)

Upvotes: 0

Joel Wigton
Joel Wigton

Reputation: 814

It can depend on the default system font, so there is no definitive answer. You'll have to find empirical values for your application and device.

I've experimentally found the fudge factor values below worked well for me.

WIDTH_FUDGE = (1/7)
HEIGHT_FUDGE = (3/4)

Multiply what you would expect the cell width and height values to be in pixels by these fudge factors to get the Excel units.

Upvotes: 2

Artem  Ayrapetov
Artem Ayrapetov

Reputation: 49

1 unit = 7 pixels. What's the logic? I don't know.

Upvotes: 4

Related Questions