Is there a way to access Slicers on Excel via Python (pywin32) - python

I am working with pywin32, workbooks for Excel. I have some charts in a sheet that i have to save. The thing is that there's a slicer in this sheet that permit to filter on a certain variable. I didn't find pywin32 docs for it and i do not have any clue here on how i can (or can't) do it..
Thanks

there is reference to Office objects. For slicers https://learn.microsoft.com/en-us/office/vba/api/excel.pivottable.slicers, be aware that slicer is an element of slicers collection
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Open("your_file.xlsx")
sl = wb.SlicerCaches("your_slicer")

Apparently in Excel, there is a program within it that could record anything you click so if you want to manipulate the filter/slicer, you can right click the element, and then choose "Assign Macro". Then you can click away as it records your clicks. Once youre done, you can view it by again choosing "Assign Macro" and a pop-up window will be available and you can choose your_filter/slicer_name_Click and it will provid you the VBA code. All you have to do is change it so it fits python format or you can implement it like this.

Related

Python: Read existing Excel file and select a different dropdown value

I want to be able to open an xlsx file in Python and select a different dropdown value in a cell which should trigger an update for the entire spreadsheet based on the new value (just how it currently does so if I manually select a different value). How can I do this in Python and which library can help me?
TL;DR: You can't.
In order to get cascading execution, you need to access the Excel execution engine. Python libraries do not have a copy of this.
If you wish to change additional values in the spreadsheet, you will need to write your Python code to make the changes.
Caveat: There technically is a way to do it using pywin32 if you have a version of Excel installed. In this case Python is simply feeding instructions to Excel, no differently than if you were using VBA. It is significantly more complicated than changing a value using a library such as Openpyxl.

Python openpyxl Table Styling convert table to range

I generate this spreadsheet via python and pandas. After generating the .xlsx file,
i would go into Excel and format it using the table Format as table button on the main ribbon.
I decided that i didn't want to manually format the spreadsheet so I started using Openpyxl to attempt to mimic my formats.
my main hiccup is that after i use the format as table button
I would then click on Convert to Range.
The problem is, i cant seem to be able to do this using openpyx.
Does anyone know how to do this? I don't want to create my own personalized style if one already exists.
this is the code i have been using
tab = Table(displayName="Final', ref='A1:O34', tableType=None)
style = TableStyleInfo(name="TableStyleLight2", showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=False)
tab.tablestyleInfo = style
ws.add_table(tab)
wb.save()
Very basic and to the point, more or less copied from teh openpyxl documents.
How do i convert this table to a range of values from openpyxl
thanks
Damn Groundhog
Have you looked at xlsxwriter? It has an option to turn the autofilter on/off when creating a table.

How to access window SmallScroll method from Python

I am new Python user and trying to get an Excel spreadsheet to scroll automatically in a loop as a video test.
Using VBA it seems that the SmallScroll method is an easy way to scroll Excel
Example:
Worksheets("Sheet1").Activate
ActiveWindow.SmallScroll down:=3
I can create an Excel worksheet form Python( Tutorial on using VBA from Python)
import win32com.client
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.visible = True
#This creates a new workbook
ExcelWorkbook = ExcelApp.Workbooks.Add()
# Add a new sheet
Excelwkrsht = ExcelWorkbook.Worksheets.Add()
However, if I try to access method
scroll1 = Excelwkrsht.SmallScroll(3)
I get an error.
Any thoughts?
Thanks.
If you google for Automate Tasks Python you will find some material.
If you are trying to automate tasks with Excel, there is a module for that.
You can install the openpyxl Module.
There is a full chapter here where you can find real nice info!
Hope that helps you!

Is there anyway to know what object does the file have in MS powerpoint or word

I use win32com to handle office document in Python.
There is no way to know what method does an Object have in win32com.
(In general, we can fetch properties or methods by DIR(OBJ) in Python )
So, if we want to know what properties or methods does my powerpoint document have , just go to look up MSDN
however , I have a powerpoint file contains lots of object.
but i don't know what name or type is it.
for example ,when i want to access text in powerpoint i can use Presentation.Slide.Textframe.TextRange.Text to access it.
How about formula??? if i want to access it?
Is there anyway when I click mouse on the object in my powerpoint file, and then show what type of the object is?
Thanks you all in advance.
For your first question you want to use the MakePy Utility to create early-bound objects so you can introspect them (ie. press tab and look at all the methods in your IDE). It will cut down searching on MSDN by 95%
Easiest way to do this is go to PythonWin (installed with win32com) and go to Tools > COM Makepy Utility and select the COM library you want to use (in your case it is something like 'Microsoft PowerPoint 14.0 Object Library'). Let this run and you are all set. This is also described here.
For your second question, as David pointed out most of the objects are Shapes. Once you run the MakePy Utility you will be able to see the entire PP Object Model. A quick search helped me find how to get the active shape that is selected by clicking on it.
import win32com.client
app = win32com.client.Dispatch("PowerPoint.Application")
selectedShape = app.ActiveWindow.Selection.ShapeRange(1)
You can now play around with selectedShape to find out everything you need to know about it.

Generating correct excel xls format

I created a little script in python to generate an excel compatible xml file (saved with xls extension). The file is generated from a part database so I can place an order with the extracted data.
On the website for ordering the parts, you can import the excel file so the order fills automatically. The problem here is that each time I want to make an order, I have to open excel and save the file with xls extension of type MS Excel 97-2003 to get the import working.
The excel document then looks exactly the same, but when opened with notepad, we cannot see the xml anymore, only binary dump.
Is there a way to automate this process, by running a bat file or maybe adding some line to my python script so it is converted in the proper format?
(I know that question has been asked before, but it never has been answered)
There are two basic approaches to this.
You asked about the first: Automating Excel to open and save the file. There are in fact two ways to do that. The second is to use Python tools that can create the file directly in Python without Excel's help. So:
1a: Automating Excel through its automation interface.
Excel is designed to be controlled by external apps, through COM automation. Python has a great COM-automation interface inside of pywin32. Unfortunately, the documentation on pywin32 is not that great, and all of the documentation on Excel's COM automation interface is written for JScript, VB, .NET, or raw COM in C. Fortunately, there are a number of questions on this site about using win32com to drive Excel, such as this one, so you can probably figure it out yourself. It would look something like this:
import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
spreadsheet = excel.Workbooks.Open('C:/path/to/spreadsheet.xml')
spreadsheet.SaveAs('C:/path/to/spreadsheet.xls', fileformat=excel.xlExcel8)
That isn't tested in any way, because I don't have a Windows box with Excel handy. And I vaguely remember having problems getting access to the fileformat names from win32com and just punting and looking up the equivalent numbers (a quick google for "fileformat xlExcel8" shows that the numerical equivalent is 56, and confirms that's the right format for 97-2003 binary xls).
Of course if you don't need to do it in Python, MSDN is full of great examples in JScript, VBA, etc.
The documentation you need is all on MSDN (since the Office Developer Network for Excel was merged into MSDN, and then apparently became a 404 page). The top-level page for Excel is Welcome to the Excel 2013 developer reference (if you want a different version, click on "Office client development" in the navigation thingy above and pick a different version), and what you mostly care about is the Object model reference. You can also find the same documentation (often links to the exact same webpages) in Excel's built-in help. For example, that's where you find out that the Application object has a Workbooks property, which is a Workbooks object, which has Open and Add methods that return a Workbook object, which has a SaveAs method, which takes an optional FileFormat parameter of type XlFileFormat, which has a value xlExcel8 = 56.
As I implied earlier, you may not be able to access enumeration values like xlExcel8 for some reason which I no longer remember, but you can look the value up on MSDN (or just Google it) and put the number 56 instead.
The other documentation (both here and elsewhere within MSDN) is usually either stuff you can guess yourself, or stuff that isn't relevant from win32com. Unfortunately, the already-sparse win32com documentation expects you to have read that documentation—but fortunately, the examples are enough to muddle your way through almost everything but the object model.
1b: Automating Excel via its GUI.
Automating a GUI on Windows is a huge pain, but there are a number of tools that make it a whole lot easier, such as pywinauto. You may be able to just use swapy to write the pywinauto script for you.
If you don't need to do it in Python, separate scripting systems like AutoIt have an even larger user base and even more examples to make your life easier.
2: Doing it all in Python.
xlutils, part of python-excel, may be able to do what you want, without touching Excel at all.

Categories

Resources