I have a python script that pastes values from one excel to another excel called 'Automate'. I have pasted the values to specific cells in sheet 1 of Automate so that sheet 2 in Automate can read the values and apply a formula. In Automate, I have a macro that uploads the values in the sheet 2 to SQL before saving. I have used openpyxl to work with excel and the function wb.save(Automate.xlsxm) doesn't run the macro.
I am able to run the below code that refreshes qand saves Automate and it runs the macro to upload the values to SQL. However, I have to manually run the script and when I use task scheduler to run the script the values do not upload values from Automate to SQL.
import win32com.client
xlapp = win32com.client.gencache.EnsureDispatch("Excel.Application")
wb = xlapp.Workbooks.Open('Automate.xlsm')
wb.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()
wb.Save()
wb.Close()
wb = None
xlapp.Quit()
xlapp = None
help will be very much appreciated
Openpyxl doesn't evaluate excel formulas and macros, as it doesn't actually use excel and doesn't have that functionality built into it. Instead, I'd recommend using xlwings, which opens excel and will evaluate all formula on opening (if they are set up to automatically reevaluate). You can also use xlwings to run your macro, with some examples here. A rough outline of your code would then be:
import openpyxl as op
import xlwings as xw
# Openpyxl open and write
wb_path = "Your_workbook.xlsm"
wb = op.load_workbook(wb_path)
ws = wb["Sheet1"]
# Write your values etc
# Save
wb.save(wb_path)
wb.close()
# Open with xlwings
wb = xw.Book(wb_path)
app = xw.apps.active
# Run macro
macro = wb.macro("YourMacro")
macro()
# Save, close
wb.save(wb_path)
app.quit()
Related
I need to open and edit my Excel with openpyxl, store the excel as a dataframe, and close the excel without any changes. Are there any ways to kill the excel and disable the auto-recovery dialogue which may pop out later?
The reason I'm asking is that my code worked perfectly fine in Pycharm, however after I packed it into .exe with pyinstaller, the code stopped working, the error said "Excel cannot access the file, there are serval possible reasons, the file name or path does not exist, or the file is being used by another program, or the workbook you are saving has the same name as a currently open workbook.
I assume it is because the openpyxl did not really close the excel, and I exported it to a different folder with the same file name.
Here is my code:
wb1 = openpyxl.load_workbook(my_path, keep_vba=True)
ws1 = wb1["sheet name"]
making changes...
ws1_df = pd.DataFrame(ws1.values)
wb1.close()
Many thanks ahead :)
The following way you can do this. solution
from win32com.client import Dispatch
# Start excel application
xl = Dispatch('Excel.Application')
# Open existing excel file
book = xl.Workbooks.Open('workbook.xlsx')
# Some arbitrary excel operations ...
# Close excel application without saving file
book.Close(SaveChanges=False)
xl.Quit()
I'm using win32com to run macros in excel and openpyxl to modify cell values. In the process of debugging, I attempted to create a simplified version of existing code but still ran into the same
[Errno 13] Permission denied:'C:\\Users\\NAME\\Desktop\\old\\Book1.xlsx'.
I believe that the error is caused by the two packages (win32com and openpyxl) opening the same file and, when attempting to save/close, cannot close the instance open in the other package.
When I attempt to save/close with openpyxl before saving/closing with win32com, I run into the permission denied error. This makes sense; Openpyxl probably does not have permission to close the excel instance open through win32com. Code is below:
wb.save(r"C:\Users\NAME\Desktop\old\Book1.xlsx")
xel.Workbooks(1).Close(SaveChanges=True)
However, when I switch the order:
xel.Workbooks(1).Close(SaveChanges=True)
wb.save(r"C:\Users\NAME\Desktop\old\Book1.xlsx")
Excel attempts to save a backup file (randomly named "522FED10" or "35C0ED10", etc.) and when I press save, Excel crashes.
What's the workaround? I was thinking that you could use win32com to run the macros, save under a different filename, then use openpyxl to access that file and edit values. However, this is extremely inefficient (I'm dealing with excel files that have hundreds of thousands of rows of data). I could consider just using win32com, but that would require a revamp of a system.
Simple code:
import openpyxl as xl
import win32com.client
xel=win32com.client.Dispatch("Excel.Application")
xel.Workbooks.Open(Filename=r"C:\Users\NAME\Desktop\old\Book1.xlsx")
wb = xl.load_workbook(r"C:\Users\NAME\Desktop\old\Book1.xlsx")
ws = wb.active
xel.visible = False
xel.Cells(1,1).Value = 'Hello Excel'
ws.cell(row = 1,column = 2).value = "test"
xel.Workbooks(1).Close(SaveChanges=True)
wb.save(r"C:\Users\NAME\Desktop\old\Book1.xlsx")
Current issue
You should definitely not mix win32com and openpyxl operations.
The win32com statement xel.Workbooks.Open() loads the workbook contents into a memory space controlled by an Excel process. The openpyxl xl.load_workbook() statement on the other hand loads the workbook contents into a completely separate memory space controlled by a Python process.
Hence any subsequent win32com commands will do nothing to affect the workbook that's living inside the python-process-controlled memory, and vice versa any openpxyl commands will do nothing to affect the workbook that's living inside the Excel-process-controlled memory.
Solution
You mentioned that you have to run some excel macros. This rules out an openpyxl-only solution. My suggestion would be to use xlwings, which is in essence a powerful and user-friendly wrapper around the win32com API.
Here is a simple example of how you can execute Excel macros and manually update cell values within a single python script:
import xlwings as xw
# Start Excel app (invisibly in the background)
app = xw.App(visible=False)
# Load excel file into active Excel app
book = app.books.open(r"Book1.xlsm")
# Instruct Excel to execute the pre-existing excel macro named "CleanUpMacro"
book.macro("CleanUpMacro")()
# Instruct Excel to write a cell value in the first sheet
book.sheets["Sheet1"].range('A1').value = 42
# Save workbook and terminate Excel application
book.save()
book.close()
app.kill()
When I open a workbook using
wbTest = xlwings.Book('test.xlsm')
the EXCEL application opens and shows the workbook. But when doing a
wbTest.close()
afterwards, the workbook closes, but the EXCEL window stays open so that I have to close it manually, even though xlwings.apps returns an empty list:
Is there a way to close the EXCEL window as soon as the last workbook closes?
In the official documentation (readthedocs) I could not find anything solving this question, so far.
On Windows, xlwings currently requires a workbook in order to communicate with Excel. But you can achieve want you want by quitting the app rather than just closing the workbook (you could check first if there are more than one workbooks open in that app via len(wbTest.app.books)):
wbTest.app.quit()
You may want to save the workbook first or alternatively there's also app.kill().
Sometimes, a loaded workbook contains macros which leave the application in an "unsaved" state, even when no changes had been made, yet. Using wbTest.app.quit(), in this case, will prompt a save dialog, which I don't want to see. Using wbTest.app.kill() would close the workbook (and close the EXCEL window), but on re-opening EXCEL, a recovery dialog for the killed wbTest workbook will be displayed, which I'd like to avoid.
So, here the overall solution which works for me:
import xlwings
import tempfile
import os
# ... some code creating at least one workbook "wbTest"
# check, if there is only one workbook left and we don't want to save it
if len(xlwings.apps) == 1:
#save the remaining workbook into temporary folder
wbTest.save(
os.path.join(
tempfile.gettempdir(),
'test.xlsm',
)
)
# close the application
wbTest.app.quit()
Thanks for all the hints.
Along with the proposed solution given by the Author, I figured out that sometimes when you have macros on the Personal Macro Workbook, the counter of opened workbooks changes. That's why I added a simple function that handles that situation.
In my case sometimes I work with multiple excel files opened and this is very handy to get rid of the workbooks that are affected by the macros but you don't want to be closed.
def quit_excel(wb):
"""wb: workbook object from xlwings"""
print(wb.app.books) # for debugging and visualization of opened workbooks
# look if PERSONAL.XLSB is in the list of books associated with the Excel App
if "PERSONAL.XLSB" in [b.name for b in wb.app.books]:
if len(wb.app.books) == 2:
print("personal, 2, quitting")
wb.app.quit()
else:
print("personal, closing")
wb.close()
else:
if len(wb.app.books) == 1:
print("no personal, 1, quitting")
wb.app.quit()
else:
print("no personal, closing")
wb.close()
I wanted share a solution that may help some users with a slight variation to the original problem. In some scenarios I had multiple excel workbooks open and before running the code therefore I only wanted to close the workbook that the script opened and not the entire excel application. Here's my solution that will work for this scenario.
wb = xw.Book(file_path)
excel_app = xw.apps.active
#
# do stuff with workbook
#
# close workbook if more then one workbook is open.
# you won't get empty grey excel app since you have another workbook open.
if xw.apps.count > 1:
wb.close()
# close excel application if only one workbook is open
else:
excel_app.quit()
I am trying to write to an Excel worksheet but the code does not do anything. I have the file name right and it correctly detects the only sheet ('Sheet1') but when I try to write to a cell nothing happens. I am running Microsoft Office 365 if that matters.
I have tried
wb = openpyxl.load_workbook('Spendings 2019.xlsx')
ws = wb.active
ws['B3'] = 4
This does not change the Excel file at all when run.
Did you read the docs? You need
print(cell.value)
As explained https://openpyxl.readthedocs.io/en/stable/usage.html
Also, if you are making changes to the spreadsheet, you need to save it
I have a simple excel file:
A1 = 200
A2 = 300
A3 = =SUM(A1:A2)
this file works in excel and shows proper value for SUM, but while using openpyxl module for python I cannot get value in data_only=True mode
Python code from shell:
wb = openpyxl.load_workbook('writeFormula.xlsx', data_only = True)
sheet = wb.active
sheet['A3']
<Cell Sheet.A3> # python response
print(sheet['A3'].value)
None # python response
while:
wb2 = openpyxl.load_workbook('writeFormula.xlsx')
sheet2 = wb2.active
sheet2['A3'].value
'=SUM(A1:A2)' # python response
Any suggestions what am I doing wrong?
It depends upon the provenance of the file. data_only=True depends upon the value of the formula being cached by an application like Excel. If, however, the file was created by openpyxl or a similar library, then it's probable that the formula was never evaluated and, thus, no cached value is available and openpyxl will report None as the value.
I have replicated the issue with Openpyxl and Python.
I am currently using openpyxl version 2.6.3 and Python 3.7.4. Also I am assuming that you are trying to complete an exercise from ATBSWP by Al Sweigart.
I tried and tested Charlie Clark's answer, considering that Excel may indeed cache values. I opened the spreadsheet in Excel, copied and pasted the formula into the same exact cell, and finally saved the workbook. Upon reopening the workbook in Python with Openpyxl with the data_only=True option, and reading the value of this cell, I saw the proper value, 500, instead of the wrong value, the None type.
I hope this helps.
I had the same issue. This may not be the most elegant solution, but this is what worked for me:
import xlwings
from openpyxl import load_workbook
excel_app = xlwings.App(visible=False)
excel_book = excel_app.books.open('writeFormula.xlsx')
excel_book.save()
excel_book.close()
excel_app.quit()
workbook = load_workbook(filename='writeFormula.xlsx', data_only=True)
I have suggestion to this problem. Convert xlsx file to csv :).
You will still have the original xlsx file. The conversion is done by libreoffice (it is that subprocess.call() line).You can use also Pandas for this as a more pythonic way.
from subprocess import call
from openpyxl import load_workbook
from csv import reader
filename="test"
wb = load_workbook(filename+".xlsx")
spread_range = wb['Sheet1']
#what ever function there is in A1 cell to be evaluated
print(spread_range.cell(row=1,column=1).value)
wb.close()
#this line can be done with subprocess or os.system()
#libreoffice --headless --convert-to csv $filename --outdir $outdir
call("libreoffice --headless --convert-to csv "+filename+".xlsx", shell=True)
with open(filename+".csv", newline='') as f:
reader = reader(f)
data = list(reader)
print(data[0][0])
or
# importing pandas as pd
import pandas as pd
# read an excel file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_excel("Test.xlsx"))
# show the dataframe
df
I hope this helps somebody :-)
Yes, #Beno is right. If you want to edit the file without touching it, you can make a little "robot" that edits your excel file.
WARNING: This is a recursive way to edit the excel file. These libraries are depend on your machine, make sure you set time.sleep properly before continuing the rest of the code.
For instance, I use time.sleep, subprocess.Popen, and pywinauto.keyboard.send_keys, just add random character to any cell that you set, then save it. Then the data_only=True is working perfectly.
for more info about pywinauto.keyboard: pywinauto.keyboard
# import these stuff
import subprocess
from pywinauto.keyboard import send_keys
import time
import pygetwindow as gw
import pywinauto
excel_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
excel_file_path = r"D:\test.xlsx"
def focus_to_window(window_title=None): # function to focus to window. https://stackoverflow.com/a/65623513/8903813
window = gw.getWindowsWithTitle(window_title)[0]
if not window.isActive:
pywinauto.application.Application().connect(handle=window._hWnd).top_window().set_focus()
subprocess.Popen([excel_path, excel_file_path])
time.sleep(1.5) # wait excel to open. Depends on your machine, set it propoerly
focus_to_window("Excel") # focus to that opened file
send_keys('%{F3}') # excel's name box | ALT+F3
send_keys('AA1{ENTER}') # whatever cell do you want to insert somthing | Type 'AA1' then press Enter
send_keys('Stackoverflow.com') # put whatever you want | Type 'Stackoverflow.com'
send_keys('^s') # save | CTRL+S
send_keys('%{F4}') # exit | ALT+F4
print("Done")
Sorry for my bad english.
As others already mentioned, Openpyxl only reads cashed formula value in data_only mode. I have used PyWin32 to open and save each XLSX file before it's processed by Openpyxl to read the formulas result value. This works for me well, as I don't process large files. This solution will work only if you have MS Excel installed on your PC.
import os
import win32com.client
from openpyxl import load_workbook
# Opening and saving XLSX file, so results for each stored formula can be evaluated and cashed so OpenPyXL can read them.
excel_file = os.path.join(path, file)
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
excel.DisplayAlerts = False # disabling prompts to overwrite existing file
excel.Workbooks.Open(excel_file )
excel.ActiveWorkbook.SaveAs(excel_file, FileFormat=51, ConflictResolution=2)
excel.DisplayAlerts = True # enabling prompts
excel.ActiveWorkbook.Close()
wb = load_workbook(excel_file)
# read your formula values with openpyxl and do other stuff here
I ran into the same issue. After reading through this thread I managed to fix it by simply opening the excel file, making a change then saving the file again. What a weird issue.