?
Solved

Save rendered HTML as a graphic file?

Posted on 2005-05-06
11
Medium Priority
?
321 Views
Last Modified: 2010-08-05
I can get (via the webbroswer module) a browser rendering some html. Now I want to save the image in a graphic file form. Is there a way to do that? Generic? Broswer specific?
0
Comment
Question by:ramrom
  • 7
  • 4
11 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13948507
You don't specify your platform.  Here's a solution for Windows - it needs ctypes (http://starship.python.net/crew/theller/ctypes/) and PIL (http://www.pythonware.com/products/pil/)

import time, webbrowser, ImageGrab
from ctypes import *
from ctypes.wintypes import *

# Show the page.
webbrowser.open("http://google.com")
time.sleep(3)

# Get the bounding rectangle of the browser.
browser = windll.user32.GetForegroundWindow()
r = RECT()
windll.user32.GetWindowRect(browser, byref(r))

# Grab the image and save it.
image = ImageGrab.grab((r.left, r.top, r.right, r.bottom))
image.save('browser.png')
0
 
LVL 17

Author Comment

by:ramrom
ID: 13948551
When I run this I get a partial image of of the browser but the rectangle shape is that of the window (Explorer, Dos prompt) from which I launch the program.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13948617
It works for me with Internet Explorer - if you're not using IE as your default browser, could you try setting it up that way?
0
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
LVL 14

Accepted Solution

by:
RichieHindle earned 1200 total points
ID: 13948646
Or try this one:

import time, win32com.client, ImageGrab
from ctypes import *
from ctypes.wintypes import *

# Show the page.
ie = win32com.client.DispatchEx("InternetExplorer.Application")
ie.Navigate("http://google.com")
ie.Visible = True
windll.user32.SetForegroundWindow(ie.HWND)
time.sleep(1)

# Get the bounding rectangle of the browser.
browser = windll.user32.GetForegroundWindow()
r = RECT()
windll.user32.GetWindowRect(browser, byref(r))

# Grab the image and save it.
image = ImageGrab.grab((r.left, r.top, r.right, r.bottom))
image.save('browser.png')
0
 
LVL 17

Author Comment

by:ramrom
ID: 13949286
I was using Mozilla. IE does work (better). However my goal (now being made more specific) is to capture the entire HTML rendering (as tho the explorer window were sized large enough as to need no scroll bars, even if that exceeded the screen size). I'm increasing points.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13949750
Changing the spec?  Fetch me the moderator!  8-)

I'll have a think about this one.
0
 
LVL 17

Author Comment

by:ramrom
ID: 13950711
I have accepted your answer to the original question. I will post the new spec as a new question.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13951036
Thanks!  (I wasn't serious, you know).
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13951761
For future readers: the follow-up question is http://Q_21416084.html
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13951764
Gah!  I meant http:Q_21416084.html
0
 
LVL 17

Author Comment

by:ramrom
ID: 13952181
"Thanks!  (I wasn't serious, you know)"  No - I didn't get that

As Page Editor I think I should "know better" exspecially when I'm asking others to start new questions.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question