八、Openpyxl工作表對象的屬性和方法-5(打印設置2/2)

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的打印設置來彌補

Pywin32操控Excel——3. 打印設置


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')


4. 打印,我在openpyxl并沒有找到打印操作,所以用pywin32來彌補。知道的可以在評論區(qū)里提出,謝謝!

Pywin32操控Excel——3. 打印設置

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容