in python we usually use dicts in such kind of case scenario
here are two implementations, the second being more modular by implementing function
Eventually, to avoid KeyError when retrieve value from page_size (i.e. hieght is not present in the dict) you may use
here are two implementations, the second being more modular by implementing function
page_sizes = {1189:'A0', 841:'A1', 594:'A2', 420:'A3', 297:'A4'} page_orientations = {True:'Portrait', False:'Landscape'} p_size = page_sizes[max(mxd.pageSize.height, mxd.pageSize.width)] p_orientation = page_orientations[mxd.pageSize.height>mxd.pageSize.width] print("Page size is {} {}".format(p_size, p_orientation))
def page_info(height, width): page_sizes = {1189:'A0', 841:'A1', 594:'A2', 420:'A3', 297:'A4'} page_orientations = {True:'Portrait', False:'Landscape'} p_size = page_sizes[max(height, width)] p_orientation = page_orientations[height>width] return {'size':p_size, 'orientation':p_orientation} p_info = page_info(height=mxd.pageSize.height, width=mxd.pageSize.width) print("Page size is {size} {orientation}".format(**p_info))
Eventually, to avoid KeyError when retrieve value from page_size (i.e. hieght is not present in the dict) you may use
p_size = page_sizes.get(max(height, width), 'Unknown') # may use None if you preferinstead of
p_size = page_sizes[max(height, width)]also note that if height=width, page_info will return "Landscape" for orientation. You may change the code if you prefer 'Portrait' in this particular case.