整理了個(gè)把小時(shí)
每次設(shè)置個(gè)打印格式來(lái)回找累死了,干脆搞出來(lái)分享給大家
轉(zhuǎn)載注明出處
import openpyxl as pyxlnfrom openpyxl.worksheet.pagebreak import Breaknwb=pyxl.Workbook()nws=wb.activen#源碼worksheet->worksheet->Worksheet()n#設(shè)置打印區(qū)域nws.print_area = 'A1:F10'nn#設(shè)置打印標(biāo)題和打印列nws.print_title_rows='1:1'nws.print_title_cols="A:B"nprint(ws.print_titles)#只讀屬性nn#凍結(jié)窗格 凍結(jié)第一行第一列nws.freeze_panes = 'B2'nn#未測(cè)試 分頁(yè)符 適用于openpyxl 3.0.4以后nrow_number=20 #需要插入分頁(yè)符的行號(hào)nnext_page_horizon, next_page_vertical = ws.page_breaks # 返回2個(gè)變量 后期版本可能取消nnext_page_horizon.append(Break(row_number)) # 通過(guò)help可以查到append屬性nn#設(shè)置打印A3橫向nws.set_printer_settings(ws.PAPERSIZE_A3,ws.ORIENTATION_LANDSCAPE)n"""n #所有默認(rèn)設(shè)置如下 worksheet 類屬性n # Paper sizen PAPERSIZE_LETTER = '1'n PAPERSIZE_LETTER_SMALL = '2'n PAPERSIZE_TABLOID = '3'n PAPERSIZE_LEDGER = '4'n PAPERSIZE_LEGAL = '5'n PAPERSIZE_STATEMENT = '6'n PAPERSIZE_EXECUTIVE = '7'n PAPERSIZE_A3 = '8'n PAPERSIZE_A4 = '9'n PAPERSIZE_A4_SMALL = '10'n PAPERSIZE_A5 = '11'nn # Page orientationn ORIENTATION_PORTRAIT = 'portrait' #縱向n ORIENTATION_LANDSCAPE = 'landscape' #橫向n"""nn#參照源碼 worksheet->page->PrintPageSetup()n#設(shè)置縮放所有列到一頁(yè),直接設(shè)置fitToWidth=True無(wú)效,需采用如下方法n#所有列設(shè)置為一頁(yè) 逆向思維,先縮放到頁(yè)面 然后適合高度改為FLASEnws.sheet_properties.pageSetUpPr.fitToPage = True#此行必須設(shè)置nws.page_setup.fitToHeight = Falsen#其他打印設(shè)置nws.page_setup.orientation = "landscape"#設(shè)置打印方向 values=("default", "portrait", "landscape")nws.page_setup.paperSize = "8" #紙張尺寸參見(jiàn)上表nws.page_setup.firstPageNumber = 1#頁(yè)碼起始頁(yè)nws.page_setup.useFirstPageNumber = True #使用起始頁(yè) 不知道啥意思 未測(cè)試nws.page_setup.paperHeight = 297#紙張高度nws.page_setup.paperWidth = 410#紙張寬度nws.page_setup.pageOrder = "downThenOver"##頁(yè)面設(shè)置->工作表->打印順序values=("downThenOver", "overThenDown") 先列后行,先行后列 nws.page_setup.usePrinterDefaults = True #使用默認(rèn)打印機(jī)nws.page_setup.blackAndWhite = True #頁(yè)面設(shè)置->工作表->單色模式nws.page_setup.draft = True #頁(yè)面設(shè)置->工作表->草稿質(zhì)量nws.page_setup.cellComments = True #頁(yè)面設(shè)置->工作表->批注和注釋values=("asDisplayed", "atEnd") 如工作表所示,工作表末尾nws.page_setup.errors = True #頁(yè)面設(shè)置->工作表->錯(cuò)誤單元格打印為values=("displayed", "blank", "dash", "NA") 顯示值,空白,--,"#N/A"nws.page_setup.horizontalDpi = True #頁(yè)面設(shè)置->工作表->打印質(zhì)量nws.page_setup.verticalDpi = True #頁(yè)面設(shè)置->工作表->打印質(zhì)量nws.page_setup.copies = True #文件->打印->份數(shù) 未測(cè)試nnn#參照源碼worksheet->header_footer->_HeaderFooterPart()n#設(shè)置頁(yè)眉 左中右 left center rightnws.oddHeader.center.text = "XXX" #文本nws.oddHeader.center.size = 24 #字號(hào)nws.oddHeader.center.font = "微軟雅黑" #字體nws.oddHeader.center.color = "000000" #16進(jìn)制RGB顏色 參照PSn#設(shè)置頁(yè)腳nws.oddFooter.center.text = "第&[Page]頁(yè) 共&[Pages]頁(yè)"nws.oddFooter.center.size = 12nws.oddFooter.center.font = "微軟雅黑"nws.oddFooter.center.color = "000000"nn#其他頁(yè)眉頁(yè)腳設(shè)置 未測(cè)試nws.differentOddEven=True #頁(yè)面設(shè)置->頁(yè)眉/頁(yè)腳->奇偶頁(yè)不同nws.differentFirst=True #頁(yè)面設(shè)置->頁(yè)眉/頁(yè)腳->首頁(yè)不同nws.scaleWithDoc=True #頁(yè)面設(shè)置->頁(yè)眉/頁(yè)腳->隨文檔自動(dòng)縮放nws.alignWithMargins=True #頁(yè)面設(shè)置->頁(yè)眉/頁(yè)腳->與頁(yè)邊距對(duì)齊nws.evenHeader.center.text="XXX" #偶數(shù)頁(yè)眉nws.evenFooter.center.text="XXX" #偶數(shù)頁(yè)腳nws.firstHeader.center.text="XXX" #奇數(shù)頁(yè)眉nws.firstFooter.center.text="XXX" #奇數(shù)頁(yè)腳nn# 頁(yè)眉頁(yè)腳自動(dòng)文本n"""nIndividual left/center/right header/footer partnnDo not use directly.nnHeader & Footer ampersand codes:nn* &A Inserts the worksheet name #工作表名n* &B Toggles bold #加粗n* &D or &[Date] Inserts the current date #日期n* &E Toggles double-underline #雙下劃線n* &F or &[File] Inserts the workbook name #文件名n* &I Toggles italic #斜體n* &N or &[Pages] Inserts the total page count #總頁(yè)碼n* &S Toggles strikethrough #刪除線n* &T Inserts the current time #當(dāng)前時(shí)間n* &[Tab] Inserts the worksheet name #當(dāng)前工作表名n* &U Toggles underline #下劃線n* &X Toggles superscript #上標(biāo)n* &Y Toggles subscript #下標(biāo)n* &P or &[Page] Inserts the current page number #當(dāng)前頁(yè)碼n* &P+n Inserts the page number incremented by n #當(dāng)前頁(yè)碼+nn* &P-n Inserts the page number decremented by n #當(dāng)前頁(yè)碼-nn* &[Path] Inserts the workbook path #當(dāng)前文件路徑n* && Escapes the ampersand character #轉(zhuǎn)義字符和符號(hào)n* &"fontname" Selects the named font #選擇字體名n* &nn Selects the specified 2-digit font point size #選擇指定的兩位字體點(diǎn)大小?nnColours are in RGB Hex #顏色是十六進(jìn)制RGBn"""nn#源碼worksheet->page->PrintOptions()n#頁(yè)面設(shè)置->頁(yè)邊距->居中方式 水平/垂直nws.print_options.horizontalCentered=Truenws.print_options.verticalCentered=Truen#未測(cè)試 nws.print_options.headings=True #頁(yè)面設(shè)置->工作表->行和列標(biāo)題nws.print_options.gridLines=True #頁(yè)面設(shè)置->工作表->網(wǎng)格線nws.print_options.gridLinesSet=True #猜不出來(lái)什么意思nnn#源碼worksheet->page->PageMargins()n#未測(cè)試 頁(yè)邊距nws.page_margins.left=0.75 #左nws.page_margins.right=0.75 #右nws.page_margins.top=1 #上nws.page_margins.bottom=1 #下nws.page_margins.header=0.5 #頁(yè)眉nws.page_margins.footer=0.5 #頁(yè)腳