Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a Class to easily connect to the active Workboook #30

Closed
fzumstein opened this issue Jun 13, 2014 · 8 comments
Closed

Provide a Class to easily connect to the active Workboook #30

fzumstein opened this issue Jun 13, 2014 · 8 comments
Milestone

Comments

@fzumstein
Copy link
Member

Probably best to make wb = ActiveWorkbook() or wb = Workbook.active() as otherwise `wb = Workbook(r'path\to\file.xls') would have to have an additional Parameter which makes it clunky.

@fzumstein fzumstein changed the title Provide a method to easily connect to the active Workboook Provide a Class to easily connect to the active Workboook Jun 16, 2014
@fzumstein
Copy link
Member Author

Can't seem to make this work if the Workbook is unsaved:

    @classmethod
    def active(cls):
        com_app = dynamic.Dispatch('Excel.Application')
        print com_app.Application.ActiveWorkbook.Name
        return cls(com_app.Application.ActiveWorkbook.Name)

@fzumstein
Copy link
Member Author

Maybe it's still worth doing it for saved spreadsheets only. Still saves you from looking up the path, and copy/pasting it.

@fzumstein fzumstein reopened this Jun 18, 2014
@fzumstein
Copy link
Member Author

For unsaved Workbooks, one can do wb = Workbook('Book1') ...

@ankostis
Copy link

ankostis commented Dec 2, 2014

This code succeeds most of the time (:-) to get an active but unsaved workbook:

import xlwings as xw

def get_active_workbook():
    from win32com.client import dynamic

    com_app = dynamic.Dispatch('Excel.Application')
    com_wb = com_app.ActiveWorkbook
    wb = xw.Workbook(xl_workbook=com_wb)

    return wb

@fzumstein
Copy link
Member Author

Oh great - yeah haven't looked at this since the implementation of xl_workbook. The only issue I've noticed so far is that it's always going against the first instance of Excel if you have various instances...

@fzumstein
Copy link
Member Author

To solve the problem with various instances, there seems to be no other solution than this: http://stackoverflow.com/a/2877943/918626

import win32gui

windows = []
win32gui.EnumWindows(lambda hwnd, resultList: resultList.append(win32gui.GetWindowText(hwnd)),windows)
#enumerates all the windows open from the top down
[i for i in windows if "Microsoft Excel" in i].pop(0)
#this one is closest to the top

then using GetObject on the name and sticking that into a Workbook as xl_workbook arg should probably work.

@fzumstein fzumstein modified the milestones: v0.3.6, v0.3.7 Jul 12, 2015
@fzumstein fzumstein removed this from the v0.3.7 milestone Aug 15, 2015
@fzumstein fzumstein added this to the v0.3.8 milestone Sep 10, 2015
@fzumstein
Copy link
Member Author

this should now be easy after the HWND refactor

@fzumstein
Copy link
Member Author

closed by ce378fd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants