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











