Sep-24-2017, 06:37 PM
I was thinking further and realised that the function (and your if/else construct to that matter) does not cover all possibilities. Here is more 'universal" one that takes into account when height and width is not a standard one, e.g. cases like height 297 (standard) and width=100, which is not a standard one.
def page_info(height, width): if height == width: p_size, p_orientation = 'Unknown', 'Square' else: p_dimensions = tuple(sorted([height, width], reverse=True)) page_sizes = {(1189, 841):'A0', (841, 594):'A1', (594, 420):'A2', (420, 297):'A3', (297, 210):'A4'} page_orientations = {True:'Portrait', False:'Landscape'} p_size = page_sizes.get(p_dimensions, 'Unknown') p_orientation = page_orientations[height>width] return {'size':p_size, 'orientation':p_orientation} for height, width in ((1189, 841), (210, 297), (200, 100), (300, 300)): p_info = page_info(height, width) print("Page size is {size}, orientation is {orientation}".format(**p_info))
Output:Page size is A0, orientation is Portrait
Page size is A4, orientation is Landscape
Page size is Unknown, orientation is Portrait
Page size is Unknown, orientation is Square