简体   繁体   中英

How to ignore “Enable Editing” in excel after writing data using openpyxl

I am using a Excel template which have 6 tabs (All unprotected) and writing the data on each worksheet using openpyxl module. Once the excel file is created and when tried to open the generated file, its not showing all data untill and unless I click "Enable editing" pop up.

Is there any attribute to disable in openpyxl.

This sounds like Windows has quarantined files received over a network. As this is done when the files are received, there is no way to avoid this when creating the files.

I solved this for me.

I found the answer here: https://codereview.stackexchange.com/questions/240136/python-script-for-refreshing-and-preprocessing-data-in-excel-report-files

I only used the refresh function and it basically opened the excel file, click/refreshed, and closed/saved. You see an Excel file appear briefly on the screen. I'll insert this in a loop to go through all the files I am creating. It might take a little while to run hundreds, but much faster than open-click-save.

Here is all the code I used:

import win32com.client as win32

def refresh(directory, file_name):
    xlapp = win32.DispatchEx('Excel.Application')
    xlapp.DisplayAlerts = False
    xlapp.Visible = True
    xlbook = xlapp.Workbooks.Open(directory + '\\' + file_name)
    xlbook.RefreshAll()
    xlbook.Save()
    xlbook.Close()
    xlapp.Quit()

    return()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM