[Python] openpyxl Page Setup "Fit To" vs "Adjust To"

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to set my worksheet to automatically fit all columns on one page using openpyxl. The way I have approached this is to set the fitToWidth property and the fitToHeight property. So far I can modify the page setup to have a fitToWidth value of 1 and the fitToHeight value of 0 but when I open the spreadsheet, it is still in the "Adjust to" selection and not the "Fit to" selection where the fitToWidth and fitToHeight values have been applied. How can I change that selection in the page setup to be "Fit to" instead of "Adjust to"?

    I think the PageSetup.zoom property which needs to be False but I can't figure out how to do that with openpyxl. Here's the MSDN article on it: https://msdn.microsoft.com/en-us/library/office/ff197028.aspx

    Here is my code so far:

    from openpyxl import load_workbook
    wb = load_workbook('test.xlsx')
    ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
    ws.page_setup.fitToWidth = 1
    ws.page_setup.fitToHeight = 0
    ws.sheet_view.view = "pageLayout"


    Thanks!

    Continue reading...

Compartilhe esta Página