[Python] How to update a cell in an Excel .xlsm file using Python without breaking print...

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have an Excel .xlsm file that includes macros, pre-configured print areas, frozen panes, repeating header rows, dropdown lists, buttons, and other formatting.

    I want to use Python to update the value of a single cell (e.g., cell A3) without breaking or resetting any of those settings — especially print settings and macros. I just want to insert a value and keep the rest of the file intact.

    Here's what I tried:

    import pythoncom
    import win32com.client

    pythoncom.CoInitialize()
    excel = win32com.client.Dispatch("Excel.Application")

    file_path = r"C:\StackOverFlow\book.xlsm"
    workbook = excel.Workbooks.Open(file_path)
    worksheet = workbook.Worksheets("sheet")

    worksheet.Range('A3').Value = 11
    workbook.Save()
    excel.Quit()


    But after saving, all the print settings (print area, repeated rows, etc.) were lost.

    How can I update the cell without affecting the rest of the workbook?

    Continue reading...

Compartilhe esta Página