• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Save rendered HTML as a graphic file?

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
ramrom
Asked:
ramrom
  • 7
  • 4
1 Solution
 
RichieHindleCommented:
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
 
ramromconsultant Author Commented:
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
 
RichieHindleCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
RichieHindleCommented:
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
 
ramromconsultant Author Commented:
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
 
RichieHindleCommented:
Changing the spec?  Fetch me the moderator!  8-)

I'll have a think about this one.
0
 
ramromconsultant Author Commented:
I have accepted your answer to the original question. I will post the new spec as a new question.
0
 
RichieHindleCommented:
Thanks!  (I wasn't serious, you know).
0
 
RichieHindleCommented:
For future readers: the follow-up question is http://Q_21416084.html
0
 
RichieHindleCommented:
Gah!  I meant http:Q_21416084.html
0
 
ramromconsultant Author Commented:
"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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now