473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Download some JPG files and make it a single PDF & share it

3 2Bits
I download some jpg files (A4 size) from a website and convert all the files into a single pdf file and email it.

The jpg files are in a url format of:

http://URL.com/RPT/2020/07/26/<page no>

The date today is 2020/7/26 and page number as 2 char (01-15)

How can I automate the whole process so it does it automatically without any thing from me?

My technical background is I learnt MS office packages in school and completed FreeCodeCamp's Responsive Web Design Certification and I am currently doing Automate the Boring Stuff with Python.

I just have a laptop, no server or any cloud accounts. So, I am hoping that I can run it program/script (not sure of the difference) on some free service.

Is Google App Script capable of doing what I want?

Pls mention the steps I need to follow or better if you can like me to a tutorial of anyone doing a project similar to mine.
Jul 27 '20 #1
7 2733
dev7060
636 Expert 512MB
Use the site's API if there is any. Or use web scraping to extract the images. Keep in mind web scraping could affect the performance of the server and may not be allowed according to the terms of the website. For pdf conversion and mailing, I'm pretty sure there are many packages already available to perform such tasks. It's just how you connect the dots and make use of em to get the final result that you want.
Jul 27 '20 #2
SioSio
272 256MB
For the sake of clarity, I coded in processing units.

In my environment, because of security reasons, I could not test only the part that downloads the jpg file, so I have not confirmed the operation.
Other than that, the operation has been confirmed.

I do not write the code of the send email part,
Use smtplib to send email, and use MIMEMultipart to attach files to emails.

Expand|Select|Wrap|Line Numbers
  1. import datetime
  2. import time
  3. import os
  4. import urllib.error
  5. import urllib.request
  6. from PIL import Image
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. from matplotlib.backends.backend_pdf import PdfPages
  10. from PyPDF2 import PdfFileMerger
  11.  
  12. #function download jpg files
  13. def download_jpg(url, dl_dir):
  14.     try:
  15.         with urllib.request.urlopen(url) as web_file, open(dl_dir, 'wb') as local_file:
  16.             local_file.write(web_file.read())
  17.             return None
  18.     except urllib.error.URLError as e:
  19.         print(e)
  20.         return None
  21.  
  22. # Set Url_list
  23. dt_now = datetime.datetime.now()
  24. yyyy = dt_now.strftime('%Y')
  25. mm = dt_now.strftime('%m')
  26. dd = dt_now.strftime('%d')
  27. url_base = 'http://URL.com/RPT/' + yyyy + '/' + mm + '/' + dd + '/'
  28. url_list = [url_base + '{:02}.jpg'.format(i) for i in range(1, 16)]
  29. img_list = ['img{:02},jpg'.format(i) ]for i in range(1, 16)
  30. pdf_list = ['img{:02},pdf'.format(i) for i in range(1, 16)]
  31.  
  32. # Download jpg files
  33. sleep_time = 1
  34. dl_dir = '.' # Specify a download folder.
  35. for url in url_list:
  36.     download_jpg(url, dl_dir)
  37.     time.sleep(sleep_time)
  38.  
  39. #convert jpg file to pdf
  40. for img in img_list:
  41.     image = Image.open(img)
  42.     image = np.asarray(image)
  43.     fig = plt.figure()
  44.     plt.axis('off')
  45.     plt.imshow(image)
  46.     pp = PdfPages(img.replace('jpg','pdf'))
  47.     pp.savefig(fig)
  48.     pp.close()
  49.  
  50. # Combine pdf files
  51. pdfmarger = PdfFileMerger()
  52. for pdf in pdf_list:
  53.     pdfmarger.append(pdf)
  54. pdfmarger.write('img-' + yyyy + '-' + mm + '-' + dd + '.pdf')
  55. pdfmarger.close()
  56.  
Jul 28 '20 #3
SioSio
272 256MB
You can find an example of sending email with python from the following URL.

https://bytes.com/searchresults.php?...ment&gsc.sort=
Jul 28 '20 #4
rompdeck
3 2Bits
wow @SioSio, thanks for the outline. I was stuck at converting to pdf.

Another thing I want to know is if I can run it on a server. As my laptop is not on always and I dont have any server. Also I have no clue how to use a server/ what is actually a server.

I am hoping to keep it running in a server so it started up and does its thing at a specific time.

Also, the sleep time is vital for not causing issue with the server? I dont understand the need to sleep.
Jul 29 '20 #5
rompdeck
3 2Bits
@dev7060 I am stuck at connecting the dots as I have no clue how to set it up on a server so it can run on its own without any issue. I have to the point it downloads the jpg files to my laptop.
Jul 29 '20 #6
dev7060
636 Expert 512MB
I suggest reading a how to guide on using python with a hosted server. Testing can always be done in the local environment.
Jul 29 '20 #7
SioSio
272 256MB
Another way to convert multiple images into one PDF using "img2pdf".
If the above way stuck at converting to pdf, try replacing "#convert jpg file to pdf" with this.
Expand|Select|Wrap|Line Numbers
  1. import os
  2. import img2pdf
  3.  
  4. pdfFileName = "output.pdf"
  5. path = "."
  6. ext = ".jpg"
  7.  
  8. with open(pdfFileName, "wb") as files:
  9.   files.write(img2pdf.convert([i for i in os.listdir(path)if i.endswith(ext)]))
Jul 30 '20 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: scrimp | last post by:
Ive been using PAMIE 1.4 to try to automate web page processes. The one thing I cannot do with it is upload files and download files. With uploading files, the file input box does not allow PAMIE...
0
by: Sachit | last post by:
I am working on an BIT application to download files from remote server using C#, There are an issue related with performance, I am downloading 3 files 28Kb, 79KB, 121KB respectivly on my...
4
by: S. Graefner | last post by:
I have been unable to locate anywhere the answer to my problem. I would like to be able to download/import a single record. This record is stored in many different related (one to one) tables. I...
2
by: Grant | last post by:
Hi, I'm trying to download files using asp.net (vb) and it seems to take forever to actually bring up the Save As dialog box. The files are Academic Software Downloads so are quite large in...
0
by: tanyali | last post by:
using php5, I download files : ******** downloadfile.php $fileContent = @mysql_result($result1,$i,"gdata"); ...
9
by: xz | last post by:
What sense do h files make in c/cpp? I never thought about this question before. Does the existence of h files make sense at all? Why do we need to declare functions in h files and...
12
nathj
by: nathj | last post by:
Hi, I am working on a system, as you may have seen from my other posts, that allows members to download files. When they download a file I want to store the user ID and the file ID in a table...
0
by: Joel Barsotti | last post by:
Currently I let people download files that the server knows they have purchased. I use Response.TransmitFile(filePath + "\\" + fileName) command filepath is usually a network share like...
5
by: nagmvs | last post by:
Hello, Can anyone tell me how can i create a button in asp and link to download files from some folder one by one Simultaneously? I wrote some code to download one file,i don't no...
3
by: vanlash | last post by:
I have an excel file that takes pasted part numbers and downloads the linked files to a local directory. I'd really like to do this in Access (2007). So the user doesn't have to use two tools to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.