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

3
  • 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. Commented May 12, 2016 at 7: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 Commented May 12, 2016 at 9:29
  • Sounds like it couldn't find the file then ;-) Try giving it a full path name. Commented May 12, 2016 at 9:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

While this may solve OP's problem, it would help to explain why OP should use this library instead of their current approach
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.

3 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.