units.py 2.6 KB

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