Python create Monthly Moon Phases & Sunrise/Sunset Times & Azimuths & Automate PDF image/watermark and url links

This one is a bit backwards. My first focus was on automating a URL link with a python script and /or a image/logo/watermark that can be positioned where you specifically want on a page. This ended up not being as easy as I’d initially thought.

fpdf library for creating url & image links in new pdfs

Creating url clickable links and clickable images in a new pdf using the fpdf and fpdf2 libraries is reasonably straightforward. The issue comes when you are trying to add these to pre-created pdfs. That is where the rub is. You can do it with paid libraries, but I wanted to do it for free.

After a lot of effort I was able to create both of these features using fpdf for a new pdf, and that was sort of OK as the csv to pdf table process that I used to make the epheremis uses fpdf library and a class written for this library to make a table. So as this is generated from scratch so you can add clickable texct using the cell method and also add clickable images (png or jpg) to add a logo or personal title to the page.

You can position text/cell or image where you want

Other python pdf libratries will allow you to overlay pdf onto an existing pdf but they DON’T allow you to position them where you want, they usually have a previous defined preset and overlaying images/logo pdfs land in these positions (eg bottom left corner).

This is not what I wanted, I wanted to be able to dictate where on the page the overlay text or image should go, and that is where fpdf works. It allows you to define the position of extra items.

Combining with other Python PDF libraries to allow you to put new links/images on exiting PDF files

In fpdf2 library you can combine 2 separate libraries to allow you to work with other pdf libraries that will merge pdfs into a newp df file and also write an image (eg logo with clickable url) or text with URL link) onto an existing page of a PDF.

At this point, I’m generating:

  1. a couple of PDF tables that are using fpdf to create the tables, so that means I can generate text links or images/logos/watermarks that can also have url links
  2. A matplotlib plot, and fpdf2 can take a matplotlib image and convert it into a pdf file, so it can also add links and images directly as a new pdf.

With the above processes already available to create extra items that I desire, I can write a SEPARATE Python script that will take the 3 new pdf’s and merge them into a single file with a different pdf library.

fallback to a pre-built page of links in a word doc saved to pdf

This would just add a separate automation to the process of developing an automation to email me an epheremis with added data , so the workflow is fine.

There was always a fallback to creating all the links and inserting all the images into a SEPARATE word document and saving that as a PDF then just appending this pdf page to merge with the final document.

To be honest it was not an elegant solution as I wanted to be able to add things to the code as required, rather than manually creating a new word/pdf document every time something changed, a more dynamic process that could be automated is what I was aiming for.

Adding Unicode/Emojis to table

This was a bit of a challenge, the existing python tablemaker class that I used for the Epheremis would not do the unicode symbols and I struggled to get Bing Crosby (aka Bing Chat) and chatGPT to be able to alter the existing table class to allow for unicode. chat GPT was especially difficult as the free version only allows 4000 characters in the answer and my code was around 8000 characters so I couldn’t get a result with the free plan.

I ended up going back to Bing Crosby (aka Bing Chat) and getting him to write a new block of code to convert a csv file data to a PDF table with unicode and it did it in about a 1/4 of the lenth of the method I was using before. I’m please with the outcome but wonder if I could get the unicode in colour. Maybe a project for the future.

Sunrise/Sunset & Moon phases

The final table came out great and I was able to add the image URL link as well so if you clicked on that it sent you to a page with data about planets and other info.

Initially Bing Crosby (aka Bing Chat) gave e some code which used the astral library for getting the data but there were discrepancies with sun rise & set times when compared to NIWA website for these same values, and it didn’t want to play well with the moon data either.

So I asked Bing Crosby (aka Bing Chat) to write the code for 30 days using the Ephem library that I was using for the main Epheremis and that worked out well. There was still a discrepancy but only in a bout 4 minutes maximum (the other was about 50 minutes discrepancy).

Again, like the epheremis, the programming to get the data was far shorter than all the other issues regarding PDF, Tables & Unicode are the things that took up most of the time

End comment

Overall I’m pleased with the result and will add this to my automation for getting the data via email.

The next thing I will move onto is doing a tidal table and maybe a chart for Lyall bay, that will be quite practical and will add another feature to my daily data email.

Going beyond that I could add general weather data, like my Zapier email? Text automation I get daily and integrate that into my automation.

I’m pleased with how this project is developing.