import openpyxl wb = openpyxl.Workbook() ws = wb.active # 1. 设置页面水平居中和竖向居中 ws.print_options.horizontalCentered = True ws.print_options.verticalCentered = False # 2.设置页眉页脚 ws.oddHeader.left.text = "Page &[Page] of &N" ws.oddHeader.left.size = 14 ws.oddHeader.left.font = "'Tahoma',Bold" # 字体的设置方法见单元格的字体设置方法 ws.oddHeader.left.color = "CC3366" # 3.设置行标题和列标题 ws.print_title_cols = 'A:B' # 设置列标题 ws.print_title_rows = '1:1' # 设置行标题 # 4.设置打印区域 ws.print_area = 'A1:F10' # 5.设置纸张方向和纸张大小 ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE ws.page_setup.paperSize = 9 ''' ORIENTATION_PORTRAIT = 'portrait' #纵向 ORIENTATION_LANDSCAPE = 'landscape' #横向 # Paper size PAPERSIZE_LETTER = '1' PAPERSIZE_LETTER_SMALL = '2' PAPERSIZE_TABLOID = '3' PAPERSIZE_LEDGER = '4' PAPERSIZE_LEGAL = '5' PAPERSIZE_STATEMENT = '6' PAPERSIZE_EXECUTIVE = '7' PAPERSIZE_A3 = '8' PAPERSIZE_A4 = '9' PAPERSIZE_A4_SMALL = '10' ''' # 6.设置页边距,单位为英寸1int=2.54cm ws.page_margins.left = 0.5 ws.page_margins.right = 0.5 ws.page_margins.top = 0.5 ws.page_margins.bottom = 0.5 ws.page_margins.header=1.5 ws.page_margins.footer=1.0 wb.save('12.xlsx') ''' 1~5资料来源: https://openpyxl.readthedocs.io/en/3.1.3/print_settings.html#headers-and-footers 6资料来源 自己查资料尝试出来的 可用的其他参数资料:https://blog.csdn.net/debrnr/article/details/124067362 '''
python openpyxl打印设置
于 2024-07-14 10:01:30 首次发布