1. PrintPageSetup类
参数:
PrintPageSetup(
orientation = NoneSet(values=("default", "portrait", "landscape"))
paperSize = Integer(allow_none=True)
scale = Integer(allow_none=True)
fitToHeight = Integer(allow_none=True)
fitToWidth = Integer(allow_none=True)
firstPageNumber = Integer(allow_none=True)
useFirstPageNumber = Bool(allow_none=True)
paperHeight = UniversalMeasure(allow_none=True)
paperWidth = UniversalMeasure(allow_none=True)
pageOrder = NoneSet(values=("downThenOver", "overThenDown"))
usePrinterDefaults = Bool(allow_none=True)
blackAndWhite = Bool(allow_none=True)
draft = Bool(allow_none=True)
cellComments = NoneSet(values=("asDisplayed", "atEnd"))
)
# 还有一些其他的参数感觉用不到,就没写了。
参数详解
- orientation 文件方向
"default", "portrait", "landscape"
- paperSize 纸张大小
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'
- scale 缩放
- fitToHeight 调整成几页搞,fitToWidth 调整成几页宽
- pageOrder 页面顺序
"downThenOver" 先列后行
"overThenDown" 先行后列
- blackAndWhite 单色打印
- draft 草稿质量
- cellComments 批注
"asDisplayed" 在显示的地方
"atEnd" 在末尾
p1
p2
实例
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append([])
ws.append(['A',1,2,3])
ws.page_setup.orientation = "landscape"
ws.page_setup.fitToHeight = 2
ws.page_setup.fitToWidth = 1
ws.page_setup.pageOrder = "overThenDown"
ws.page_setup.blackAndWhite = True
ws.page_setup.draft = True
ws.page_setup.paperSize = '9' # A4纸
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
缺点
这两行代码,设置了但是没有效果
ws.page_setup.fitToHeight = 2
ws.page_setup.fitToWidth = 1
pywin32的打印设置来弥补
2. PrintOptions类
参数:
horizontalCentered = Bool(allow_none=True)
verticalCentered = Bool(allow_none=True)
headings = Bool(allow_none=True)
gridLines = Bool(allow_none=True)
实例
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append([])
ws.append(['A',1,2,3])
ws.print_options.horizontalCentered = True
ws.print_options.verticalCentered = True
ws.print_options.headings = True
ws.print_options.gridLines = True
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
3. 设置打印区域以及打印标题
ws.print_area # 打印区域
ws.print_title_rows # 打印标题行
ws.print_title_cols # 打印标题列
实例
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append([])
ws.append(['A',1,2,3])
ws.print_title_rows = "1:1"
ws.print_title_cols = "A:A"
ws.print_area = "A1:D8"
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
网友评论