units.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (c) 2010-2024 openpyxl
  2. import math
  3. #constants
  4. DEFAULT_ROW_HEIGHT = 15. # Default row height measured in point size.
  5. BASE_COL_WIDTH = 8 # in characters
  6. DEFAULT_COLUMN_WIDTH = BASE_COL_WIDTH + 5
  7. # = baseColumnWidth + {margin padding (2 pixels on each side, totalling 4 pixels)} + {gridline (1pixel)}
  8. DEFAULT_LEFT_MARGIN = 0.7 # in inches, = right margin
  9. DEFAULT_TOP_MARGIN = 0.7874 # in inches = bottom margin
  10. DEFAULT_HEADER = 0.3 # in inches
  11. # Conversion functions
  12. """
  13. From the ECMA Spec (4th Edition part 1)
  14. Page setup: "Left Page Margin in inches" p. 1647
  15. Docs from
  16. http://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
  17. See also http://msdn.microsoft.com/en-us/library/dd560821(v=office.12).aspx
  18. dxa: The main unit in OOXML is a twentieth of a point. Also called twips.
  19. pt: point. In Excel there are 72 points to an inch
  20. hp: half-points are used to specify font sizes. A font-size of 12pt equals 24 half points
  21. pct: Half-points are used to specify font sizes. A font-size of 12pt equals 24 half points
  22. EMU: English Metric Unit, EMUs are used for coordinates in vector-based
  23. drawings and embedded pictures. One inch equates to 914400 EMUs and a
  24. centimeter is 360000. For bitmaps the default resolution is 96 dpi (known as
  25. PixelsPerInch in Excel). Spec p. 1122
  26. For radial geometry Excel uses integer units of 1/60000th of a degree.
  27. """
  28. def inch_to_dxa(value):
  29. """1 inch = 72 * 20 dxa"""
  30. return int(value * 20 * 72)
  31. def dxa_to_inch(value):
  32. return value / 72 / 20
  33. def dxa_to_cm(value):
  34. return 2.54 * dxa_to_inch(value)
  35. def cm_to_dxa(value):
  36. emu = cm_to_EMU(value)
  37. inch = EMU_to_inch(emu)
  38. return inch_to_dxa(inch)
  39. def pixels_to_EMU(value):
  40. """1 pixel = 9525 EMUs"""
  41. return int(value * 9525)
  42. def EMU_to_pixels(value):
  43. return round(value / 9525)
  44. def cm_to_EMU(value):
  45. """1 cm = 360000 EMUs"""
  46. return int(value * 360000)
  47. def EMU_to_cm(value):
  48. return round(value / 360000, 4)
  49. def inch_to_EMU(value):
  50. """1 inch = 914400 EMUs"""
  51. return int(value * 914400)
  52. def EMU_to_inch(value):
  53. return round(value / 914400, 4)
  54. def pixels_to_points(value, dpi=96):
  55. """96 dpi, 72i"""
  56. return value * 72 / dpi
  57. def points_to_pixels(value, dpi=96):
  58. return int(math.ceil(value * dpi / 72))
  59. def degrees_to_angle(value):
  60. """1 degree = 60000 angles"""
  61. return int(round(value * 60000))
  62. def angle_to_degrees(value):
  63. return round(value / 60000, 2)
  64. def short_color(color):
  65. """ format a color to its short size """
  66. if len(color) > 6:
  67. return color[2:]
  68. return color