2

I am trying to copy an image from an excel named Inputs_v3 and sheet named Inputs and save. The code is as follows`

import win32com.client as win32       
from PIL import ImageGrab 
from xlrd import open_workbook   
import os

excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = open_workbook('Inputs_v3.xlsm')
r = wb.sheet_by_name('Inputs')
r.CopyPicture()

im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')

` The error is as follows

'Attribute error: 'Sheet' object has no attribute 'CopyPicture''

Please suggest where I am doing wrong.Thanks in advance

Goutham Pathuri
  • 45
  • 1
  • 1
  • 7
  • You're mixing up win32com calls with the `xlrd` package, which have nothing to do with each other. You've set `excel` to be a reference to the Excel app but then you don't do anything with it. I'm not fully familiar with either xlrd or the Excel COM model but it looks as if `CopyPicture` is a method you need to call on the Excel worksheet object, not the xlrd `sheet_by_name` object. If you can get the picture using xlrd do that and don't use COM, otherwise use COM and you don't need xlrd. – nekomatic May 12 '16 at 07:54
  • Hi..I have tried to open the work book using win32.com by excel = win32.gencache.EnsureDispatch("Excel.Application") wb = excel.Workbooks.Open("Inputs_v3.xlsm")...but it is showing that we couldn't find the file – Goutham Pathuri May 12 '16 at 09:29
  • Sounds like it couldn't find the file then ;-) Try giving it a full path name. – nekomatic May 12 '16 at 09:39

2 Answers2

6

Use a python library called excel2img. In one line you can take a screenshot from any excel file

import excel2img
excel2img.export_img("Excel File Full Path", "Target Image full Path", "Excel SheetName", None)

and you can identify a specific cells range as well.

import excel2img
excel2img.export_img("test.xlsx", "test.bmp", "", "Sheet2!B2:C15")

I hope this will help.

Agnel Vishal
  • 564
  • 6
  • 13
user2301624
  • 61
  • 1
  • 1
2

The following code will get you the win32com reference that you actually need to access the Excel worksheet's objects and methods:

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('myworkbook.xlsx')
ws = wb.Worksheets('worksheet_name')         # alternatively Worksheets(1) etc

Now you can do, for example:

ws.Shapes(1).CopyPicture()

I've tested this with Python 3.4, pywin32 219 and Excel 2010 on Windows 7.

Note that this doesn't involve xlrd at all - that's a package that can read Excel files without having Excel installed on the computer, but I don't know if it supports getting images of or from Excel workbooks.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
  • Thanks.. I need to get a particular picture on the worksheet. According to your code(range) it is taking image of entire screen in the range and in the code it is giving the error: 'Nonetype object has no attribute save' . I guess the code is not detecting the image on the clipboard.But image is getting copied on the clipboard. Can you suggest any solution for this problem? – Goutham Pathuri May 12 '16 at 18:23
  • See edited answer to copy picture, rather than image of worksheet. I don't know PIL and can't test it as I don't have Python 2.x installed - I suggest you do some more research and experimentation on that and if you don't get anywhere, ask a new question specifically about that. – nekomatic May 13 '16 at 13:17
  • http://stackoverflow.com/questions/17282278/python-save-xlpicture-from-clipboard might be relevant? – nekomatic May 13 '16 at 13:21