I have a Form with a CommandButton in my excel sheet and I would like to click it with my python script.
Things that do NOT work:
import win32com.client
xl = win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(file)
xl.Application.Run('CommandButton.1')
I am guessing this does not work as it is not a macro
import win32com.client
xl = win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(file)
xl.DoCmd.OpenForms('CommandButton.1')
xl.Forms('CommandButton.1').CommandButton_Click()
This did not work because DoCmd is for Access and does not apply to Excel.
Related
Rather than running a macro already built into an Excel spreasheet, I would like to run a single line of code. For example:
import os
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename='foo.xlsx')
xl.Application.Run("excelsheet.xlsm!modulename.macroname") # what I can do
xl.Application.RunLine('ThisWorkbook.ContentTypeProperties("Project Title") = "Category A"') # this is what I want to do instead
xl.Application.Save()
xl.Application.Quit()
del xl
Open excel,then click file->New->Blank workbook, we can get a blank workbook. We can input some information.
I want to use Python to save the created Excel to the path we set.
import win32com.client
import os
excel = win32com.client.Dispatch("Excel.Application")
xlsx_fullname = os.path.abspath("Book1")
workBook = excel.Workbooks.Open(xlsx_fullname)
excel.ExecuteExcel4Macro("FDSFORCERECALC(False)")
workBook.Save()
workBook.Close(True)
excel.Application.Quit()
I used this code, but I can't use python to select this excel file. It's not working
Change
xlsx_fullname = os.path.abspath("Book1")
workBook = excel.Workbooks.Open(xlsx_fullname)
...
workBook.Save()
to
workBook = excel.ActiveWorkbook
...
workBook.SaveAs(Filename="c:/path/to/your desired filename.xlsx")
I'm having an issue saving my file to a certain location on my Raspberry PI (Raspbian) computer. I'm wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Script. When I do wb.save("FileName.xlsx"), It only saves it to the location where the Python Script is located.
Here's my code:
from openpyxl import Workbook
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('FileName.xlsx')
Okay for any user, you can write
from openpyxl import Workbook
import getpass
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('/home/'+getpass.getuser()+'/Desktop/FileName.xlsx')
On windows systems: first you must copy the path, for example this path:
C:\Users\obada yahya\Desktop\python
Now you must add another \ after each already existing \ to the path:
import openpyxl as xl
wb=xl.Workbook()
wb.save("C:\\Users\\obada yahya\\Desktop\\python\\obada12.xlsx")
Now it will work fine.
Is there a way to simplify this with same kind with os.walk or glob?
from win32com.client import Dispatch
inputwb1 = "D:/apera/Workspace/Sounding/sounding001.xlsx"
inputwb2 = "D:/apera/Workspace/Sounding/sounding002.xlsx"
Sheet = 'OUTPUT'
excel = Dispatch("Excel.Application")
source = excel.Workbooks.Open(inputwb1)
source.Worksheets(Sheet).Range('F1:H500').Copy()
source.Worksheets(Sheet).Range('I1:K500').PasteSpecial(Paste=-4163)
source = excel.Workbooks.Open(inputwb2)
source.Worksheets(Sheet).Range('F1:H500').Copy()
source.Worksheets(Sheet).Range('I1:K500').PasteSpecial(Paste=-4163)
because this thing will take so much space if I want to write hundreds of it.
You seem to have almost answered your own question. Something like this might do it:
import glob
from win32com.client import Dispatch
Sheet = 'OUTPUT'
excel = Dispatch("Excel.Application")
for filename in glob.glob("D:/apera/Workspace/Sounding/sounding*.xlsx"):
source = excel.Workbooks.Open(filename)
source.Worksheets(Sheet).Range('F1:H500').Copy()
source.Worksheets(Sheet).Range('I1:K500').PasteSpecial(Paste=-4163)
you can open multiple excel files like this (example) :
import win32com.client
import os
path = ('D:\\New Folder\\MyExcelFiles\\')
fileslist = os.listdir('D:\\New Folder\\MyExcelFiles\\')
xl = win32com.client.DispatchEx('Excel.Application')
xl.Visible = True
for i in fileslist :
xl.Workbooks.Open(path+i)
if xl.Cells.Find('2014'):
xl.Cells.Replace('2015')
xl.Save()
xl.Workbooks.Close()
else:
xl.Workbooks.Close()
I am using WIN32com to use excel. I need to make sure that excel opens up new instances everytime. So if I run this and already have excel open I need it to open a new excel, and with in the script I need it to open a 2nd excel window for file 2 . This is what I am using: I can get it to open but not in new instances.
import win32com.client
import os
x1 = win32com.client.Dispatch("Excel.Application")
wb1= x1.workbooks.Open("X:\File1.xlsx")
wb2 = x1.workbooks.Open("X:\File2.xlsm")
x1.close("X:\File1.xlsx")
You can use DispatchEx to create a new instance of the application.
x1 = win32com.client.DispatchEx("Excel.Application")
x2 = win32com.client.DispatchEx("Excel.Application")
wb1 = x1.Workbooks.Open(.....
wb2 = x2.Workbooks.Open(.....
Roger