Posts

Showing posts from September, 2020

How To Convert Html File/URL/String Into Image Using Python

   I worked on converting HTML formate to PNG Image using python, its take 1 week to complete, First, I tried with  Imgkit  Package then I tried with  Html2image  Package, Later I tried with  Pypperteer  Package, When I used Imgkit package, I faced on one issue what it means, in HTML file we have any embedded code or more loading javascript code, its take more than 5 sec so when I used imgkit it is not taking those things on the image. when I used Html2image Package, I faced the same issue Finally, I solved that loading issue using Pypperteer Package ( https://github.com/pyppeteer/pyppeteer ), In Pypperteer Package we have one option, by using that option we can stop converting into the image until load the complete HTML file, And here we have one more great option to pass HTML string to get Document Elements like OffsetHight, etc. import asyncio from pyppeteer import launch def converting_html_image(input_url) -> str:     file_na...

How to Insert/Copy content from One Html File to Other HTML File

  How to Insert/Copy content from One Html File to Other HTML File os.path.join(os.getcwd()) -> this will ge curent working directory path (sample HTML file content) content = '''<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1">     <title>test</title>     <style>*{ margin: 0; padding: 0; width: 360px!important; max-width: 360px !important; }     </style> </head> <body> <div> <p>hello , This is paragraph content for testing purpose only</p> <script> </script>     <blockquote class="twitter-tweet" lang="en"> <a href="https://twitter.com/faniskoengage/status/1303453019204452352"></a> </blockquote> <script src="https://platform.twitter.com/widgets.js" charset="utf-8"><...

How to Crop or Resize the Image using python Script

Input - you need to pass input as a file path and save the out on the same path Ouput - Return True import os from PIP import Image  def compress_Me(file):      filepath = os.path.join(os.getcwd(), file)      oldsize = os.stat(filepath).st_size -    picture = Image.open(filepath) -    dim = picture.size -    top = 0 -    left = 0 -    right = 360 -    bottom = dim[1] -    cp = picture.crop((left, top, right, bottom)) -    cp.save(file, "PNG") -    newsize = os.stat(os.path.join(os.getcwd(), file)).st_size -    percent = (oldsize - newsize) / float(oldsize) * 100 -    print("File compressed from {0} to {1} or {2}%".format(oldsize, newsize, percent)) -    return True

Solution - Typeerror '<' not supported between instances of 'datetime.datetime' and 'str' while Sorted()

sorted(self.total_played_game_details, key=lambda i: i['start_datetime'], reverse=True) Type error '<' not supported between instances of 'datetime.datetime' and 'str'  Solution from datetime import datetime sorted(self.total_played_game_details, key=lambda i: datetime.strptime(str(i['start_datetime']), "%Y-%m-%d %H:%M:%S"))