import openpyxl as pyxl
from openpyxl.worksheet.pagebreak import Break
wb=pyxl.Workbook()
ws=wb.active
#源码worksheet->worksheet->Worksheet()
#设置打印区域
ws.print_area = 'A1:F10'
#设置打印标题和打印列
ws.print_title_rows='1:1'
ws.print_title_cols="A:B"
print(ws.print_titles)#只读属性
#冻结窗格 冻结第一行第一列
ws.freeze_panes = 'B2'
#未测试 分页符 适用于openpyxl 3.0.4以后
row_number=20 #需要插入分页符的行号
next_page_horizon, next_page_vertical = ws.page_breaks # 返回2个变量 后期版本可能取消
next_page_horizon.append(Break(row_number)) # 通过help可以查到append属性
#设置打印A3横向
ws.set_printer_settings(ws.PAPERSIZE_A3,ws.ORIENTATION_LANDSCAPE)
"""
#所有默认设置如下 worksheet 类属性
# 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'
PAPERSIZE_A5 = '11'
# Page orientation
ORIENTATION_PORTRAIT = 'portrait' #纵向
ORIENTATION_LANDSCAPE = 'landscape' #横向
"""
#参照源码 worksheet->page->PrintPageSetup()
#设置缩放所有列到一页,直接设置fitToWidth=True无效,需采用如下方法
#所有列设置为一页 逆向思维,先缩放到页面 然后适合高度改为FLASE
ws.sheet_properties.pageSetUpPr.fitToPage = True#此行必须设置
ws.page_setup.fitToHeight = False
#其他打印设置
ws.page_setup.orientation = "landscape"#设置打印方向 values=("default", "portrait", "landscape")
ws.page_setup.paperSize = "8" #纸张尺寸参见上表
ws.page_setup.firstPageNumber = 1#页码起始页
ws.page_setup.useFirstPageNumber = True #使用起始页 不知道啥意思 未测试
ws.page_setup.paperHeight = 297#纸张高度
ws.page_setup.paperWidth = 410#纸张宽度
ws.page_setup.pageOrder = "downThenOver"##页面设置->工作表->打印顺序values=("downThenOver", "overThenDown") 先列后行,先行后列
ws.page_setup.usePrinterDefaults = True #使用默认打印机
ws.page_setup.blackAndWhite = True #页面设置->工作表->单色模式
ws.page_setup.draft = True #页面设置->工作表->草稿质量
ws.page_setup.cellComments = True #页面设置->工作表->批注和注释values=("asDisplayed", "atEnd") 如工作表所示,工作表末尾
ws.page_setup.errors = True #页面设置->工作表->错误单元格打印为values=("displayed", "blank", "dash", "NA") 显示值,空白,--,"#N/A"
ws.page_setup.horizontalDpi = True #页面设置->工作表->打印质量
ws.page_setup.verticalDpi = True #页面设置->工作表->打印质量
ws.page_setup.copies = True #文件->打印->份数 未测试
#参照源码worksheet->header_footer->_HeaderFooterPart()
#设置页眉 左中右 left center right
ws.oddHeader.center.text = "XXX" #文本
ws.oddHeader.center.size = 24 #字号
ws.oddHeader.center.font = "微软雅黑" #字体
ws.oddHeader.center.color = "000000" #16进制RGB颜色 参照PS
#设置页脚
ws.oddFooter.center.text = "第&[Page]页 共&[Pages]页"
ws.oddFooter.center.size = 12
ws.oddFooter.center.font = "微软雅黑"
ws.oddFooter.center.color = "000000"
#其他页眉页脚设置 未测试
ws.differentOddEven=True #页面设置->页眉/页脚->奇偶页不同
ws.differentFirst=True #页面设置->页眉/页脚->首页不同
ws.scaleWithDoc=True #页面设置->页眉/页脚->随文档自动缩放
ws.alignWithMargins=True #页面设置->页眉/页脚->与页边距对齐
ws.evenHeader.center.text="XXX" #偶数页眉
ws.evenFooter.center.text="XXX" #偶数页脚
ws.firstHeader.center.text="XXX" #奇数页眉
ws.firstFooter.center.text="XXX" #奇数页脚
# 页眉页脚自动文本
"""
Individual left/center/right header/footer part
Do not use directly.
Header & Footer ampersand codes:
* &A Inserts the worksheet name #工作表名
* &B Toggles bold #加粗
* &D or &[Date] Inserts the current date #日期
* &E Toggles double-underline #双下划线
* &F or &[File] Inserts the workbook name #文件名
* &I Toggles italic #斜体
* &N or &[Pages] Inserts the total page count #总页码
* &S Toggles strikethrough #删除线
* &T Inserts the current time #当前时间
* &[Tab] Inserts the worksheet name #当前工作表名
* &U Toggles underline #下划线
* &X Toggles superscript #上标
* &Y Toggles subscript #下标
* &P or &[Page] Inserts the current page number #当前页码
* &P+n Inserts the page number incremented by n #当前页码+n
* &P-n Inserts the page number decremented by n #当前页码-n
* &[Path] Inserts the workbook path #当前文件路径
* && Escapes the ampersand character #转义字符和符号
* &"fontname" Selects the named font #选择字体名
* &nn Selects the specified 2-digit font point size #选择指定的两位字体点大小?
Colours are in RGB Hex #颜色是十六进制RGB
"""
#源码worksheet->page->PrintOptions()
#页面设置->页边距->居中方式 水平/垂直
ws.print_options.horizontalCentered=True
ws.print_options.verticalCentered=True
#未测试
ws.print_options.headings=True #页面设置->工作表->行和列标题
ws.print_options.gridLines=True #页面设置->工作表->网格线
ws.print_options.gridLinesSet=True #猜不出来什么意思
#源码worksheet->page->PageMargins()
#未测试 页边距
ws.page_margins.left=0.75 #左
ws.page_margins.right=0.75 #右
ws.page_margins.top=1 #上
ws.page_margins.bottom=1 #下
ws.page_margins.header=0.5 #页眉
ws.page_margins.footer=0.5 #页脚
openpyxl打印设置
最新推荐文章于 2024-07-14 16:48:43 发布