1. PrintPageSetup類
參數(shù):
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"))
)
# 還有一些其他的參數(shù)感覺用不到,就沒寫了。
參數(shù)詳解
- 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 調(diào)整成幾頁搞,fitToWidth 調(diào)整成幾頁寬
- pageOrder 頁面順序
"downThenOver" 先列后行
"overThenDown" 先行后列
- blackAndWhite 單色打印
- draft 草稿質(zhì)量
- 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類
參數(shù):
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. 設置打印區(qū)域以及打印標題
ws.print_area # 打印區(qū)域
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')