I have a python script that every morning opens an excel file, refreshes it and saves as new file with todays date. Even though no changes occur between the .SaveAs and .Close and despite all precausions taken to not display save prompt, excel of course display a dialog prompting me to save or not before close. And to make this a bit more interesting, this doesn't happen every time, sometimes it can run for weeks without it happening, and then one day it just doesn't work.
Code sample:
import os
import datetime
import win32com.client as win32
from pywinauto.application import Application
# set todays date
dt = datetime.date.today()
# connect to Excel instance
xl = win32.dynamic.Dispatch("Excel.Application")
# open the excel template
wb = xl.Workbooks.Open([path to excel file])
# set report name
report_name = "Pending Report - INTERNAL - " + str(dt) + ".xlsx"
# refresh data
wb.RefreshAll()
# save as new file with todays date
wb.SaveAs(os.path.normpath(path + report_name))
# set excel not to display alerts - to not prompt for save before close
xl.DisplayAlerts = False
# disable Events - to not prompt for save before close
xl.EnableEvents = False
# set state of workbook to Saved to make sure excel sees the file as saved
xl.Workbooks(report_name).Saved = True
# close file and save changes set to true, ie. save the changes upon close
xl.Workbooks(report_name).Close(SaveChanges=1)
# looks for the save prompt - but the code never gets here, it halts on previous line waiting for input on dialog window
try:
app = Application(backend="uia").connect(title_re=".*Workbook Before Close*")
txt = app[u"Dialog"][u"Static"].window_text()
if "Do you want to save the changes you made" in txt:
app[u"Dialog"].YesButton.click()
except:
log("l", "i", "Didn't find excel or dialog window, might not be an issue but the Save dialog check wasn't successful")
Anyone have any ideas, or know what I'm doing wrong here?
Just to be fully clear, I've tested different combinations of these xl.DisableEvents etc. also tried them separately, some seem to work for a while then one day suddenly it doesn't anymore. Since this is an automated scheduled task, when it halts, it just sits there until I get an angry email asking for the report.
Now I know a workaround would be to create another scheduled task that runs another py file, that checks for the dialog, but that's not the issue here, what I want to get working is excel to do as instructed - i.e. not displaying the save before close prompt.
Thanks everyone.
Try the following:
xl = win32.gencache.EnsureDispatch('Excel.Application')
Related
How Can i loop back the python script from particular line. This is about the refreshing an excel and I want to refresh it once in 2 hours.
import win32com.client as win32
import time
xlapp = win32.DispatchEx('Excel.Application')
wb = xlapp.Workbooks.Open('Excel path')
xlapp.visible = True
wb.RefreshAll()
time.sleep(10)
wb.Save()
wb.Close()
xlapp.Quit()
I don't want to close the excel file. So basically save it and loop back again after 2 hours and refresh and save... Repeat
How can I don that?
Since you've already imported time, you could build a loop with time.sleep(7200) (7200 seconds being two hours) that keeps saving. Although I don'T think it's a good idea to keep the file open throughout that time. What speaks against closing and re-opening it after two hours?
My Excel file when opened will need 30 seconds to load data from a database.
My script below successfully Open, Wait till data fully loaded then Save and Close Excel file one by one.
So my question is: Does python by default must wait till the workbook is fully load before it move to the next command Save(). And then wait until Save() is completed to execute Close().
I don't mind to put time.sleep(10) but I want to know how python processes the command sequence in this case.
(When I do parsing data from web, I need to command Python to wait for a certain element is loaded. That's why I wonder if I need to instruct Python to do the same with Excel.)
files = ['file1','file2']
path = f'C:\\Users\\Myfolder\\'
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
for i in files:
print(i)
wb = excel.Workbooks.Open( path + str(i)+'.xlsx')
# time.sleep(25)
for wb_name in excel.Workbooks: #list all currently open excel file
if wb_name.Name == str(i)+'.xlsx' : #Save exact file name
print("Save workbook:",wb_name.Name)
wb_name.Save()
wb_name.Close()
No python by default do not wait till the workbook is fully load before it move to the next command Save(). And rushes to the next before Save() is completed to execute Close(). THerefore, I recommend you to put delay time. Usually, the sleep time demands on the machine processing speed. In your case, it should time.sleep(35). The extra five second is just for safety.
for i in files:
print(i)
wb = excel.Workbooks.Open( path + str(i)+'.xlsx')
time.sleep(35) # yes you need it. Otherwise error message will come out.
I want to open an xlsm file via xlwings and then edit it and save it. However, some problems arose.
If I run the code with no excel file working, or just open another excel file and do not edit the excel file, it works fine. However, if I open an Excel file and do some work, for example open a blank Excel file and enter 'test' in cell A1, and run the code, sometimes it works, but sometimes it becomes unresponsive in the third line.(wb_xl = xw.Book(copy)) In this case, the code does not jump from the third line in an unresponsive state. What makes more sense is that the code works fine in some cases.
I want to know when the code works fine in all cases.
And there is one more problem.
If this code is executed while working with another Excel, only wb_xl should be terminated. I don't want another Excel to be closed. I want to exit only wb_xl. However, when the app.quit() code is executed, all open Excels are closed. In this case, how can I close only the Excel(wb_xl) opened through the code without closing the working Excel?
import xlwings as xw
copy = 'C:/Users/ijung/Desktop/210919_Mk_Lot_test/210922_101test.xlsm'
wb_xl = xw.Book(copy) #sometimes no response in this line
ws_xl = wb_xl.sheets['Main']
app = xw.apps.active
ws_xl.range('A1').value = 'test'
wb_xl.save()
app.quit()
#wb_xl.app.kill()
#wb_xl.close()
I also used openpyxl. However, in this part of wb_open.save(copy), an error such as xml.etree.ElementTree.ParseError: mismatched tag: line 20, column 8 occurred. When I use xlsx, the save works fine, but when I use xlsm, an error occurs.
import openpyxl
wb_open = openpyxl.load_workbook(copy, read_only = False, keep_vba = True)
ws_open = wb_open.active
ws_open.cell(1,1).value = 'test'
wb_open.save(copy) #error
wb_open.close()
As a result, the purpose of this code is to open the xlsm file by executing this code even when working with another Excel, edit and save, and close only this xlsm file.However, using multiple packages and searching multiple sites could not solve the problem.I'm under a lot of stress with this issue. Any help would be greatly appreciated. Please help me.
Thanks in advance.
openpyxl does not works with xlsm files that contains form objects
I think the problem is in app.quit() you are closing the excel instance, just use wb_xl.close()
import xlwings as xw
copy = 'C:/Users/ijung/Desktop/210919_Mk_Lot_test/210922_101test.xlsm'
wb_xl = xw.Book(copy) #sometimes no response in this line
ws_xl = wb_xl.sheets['Main']
#app = xw.apps.active # don't needed
ws_xl.range('A1').value = 'test'
wb_xl.save()
wb_xl.close()
This should only close the book, take a look this post has insteresting answers
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()
A file which has data I need to access is being generated in xlsx format yet I need it to be in xls format for my purpose.
I'm able to use win32com.client to open the file however the save cannot fully complete due to Compatibility Checker dialog pop up which notifies you of loss of formatting/features by going from a xlsx --> xls format.
Here's the code which currently doesn't allow me to save the file as execution hangs waiting for the dialog to close, any help would be much appreciated:
excel = win32com.client.Dispatch('Excel.Application')
excel.DisplayAlerts = False
in_file = u"C:\\Path\\to\\infile\\infile.xlsx"
out_file = u"C:\\Path\\to\\outfile\\outfile.xls"
wb = excel.Workbooks.Open(in_file)
wb.CheckCompatibility = False
wb.DoNotPromptForConvert = True
wb.SaveAs(out_file, FileFormat=56) #Execution hangs here
wb.Close()
excel.Quit()
I've seen other similar posts which mention the methods and attributes I've already set in this script. I've also modified my the registry value to ShowCompatDialog = 0.
MSDN says on Workbook.DoNotPromptForConvert property:
"true to prompt the user to convert the workbook; otherwise, false".
Write in your code:
wb.DoNotPromptForConvert = False
UPDATE: Solved this issue using this Excel Add-In
For reference: Changing the registry value was working however the values were being reset daily on the internal network which I develop in. Without being able to edit the registry values myself b/c I don't possess the admin rights to do so, the above solution was the only thing that ended up solving my problem.