def gen_report(uid,field_address,field_lat,field_long,imagedate,fieldid,field_area,lang): from docx import Document from docx.shared import Inches from docx.enum.text import WD_ALIGN_PARAGRAPH from create_bargraph import create_bargraph from create_linegraph import create_linegraph import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import datetime from get_weather_data import get_weather_data from datetime import date from docx.shared import Pt from create_temperature_bargraph import create_temperature_bargraph from create_weather_line_graph import create_weather_line_graph import cv2 from PIL import Image from create_index_piechart import create_index_piechart from get_weather_forecast import get_weather_forecast from column_img import column_img import csv #from docx_to_pdf import docx_to_pdf # Fetch the service account key JSON file contents cred = credentials.Certificate('servicekey.json') try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') lang_num = 0 all_strings = [] row_num = 0 with open('report_strings.csv') as csv_file: csv_reader = csv.reader(csv_file,delimiter=',') strings_column = [] for row in csv_reader: if row_num == 0: row_num = row_num + 1 available_langs = row for singleLang in available_langs: print(('aa'+singleLang+'aa')) print(('bb'+lang+'bb')) if lang.find(singleLang)>-1: print('lang break') break else: lang_num = lang_num+1 all_strings.append(row) print('lang num') print(lang) if lang == 'ar': lang_num = 2 else: lang_num = 0 print(lang_num) today = date.today() date_d = today.strftime('%Y%m%d') year = date_d[0:4] month = date_d[4:6] day = date_d[6:] report_date = year+'-'+month+'-'+day+' (in yyyy-mm-dd)' pageno = 1 sensed_year = imagedate[0:4] sensed_month = imagedate[4:6] sensed_day = imagedate[6:] sensed_imagedate = sensed_year+'-'+sensed_month+'-'+sensed_day+' (in yyyy-mm-dd)' sensed_day = int(sensed_day) sensed_month = int(sensed_month) print('report_date') print(report_date) print(sensed_imagedate) tci_image = uid+'/TCI.png' etci_image = uid+'/ETCI.png' ndvi_image = uid+'/ndvi.png' evi_image = uid+'/evi.png' ndwi_image = uid+'/ndwi.png' ndre_image = uid+'/ndre.png' vari_image = uid+'/vari.png' soc_image = uid+'/soc.png' ndvi_image_cmap2 = uid+'/ndvi_cmap2.png' evi_image_cmap2 = uid+'/evi_cmap2.png' ndwi_image_cmap2 = uid+'/ndwi_cmap2.png' ndre_image_cmap2 = uid+'/ndre_cmap2.png' vari_image_cmap2 = uid+'/vari_cmap2.png' soc_image_cmap2 = uid+'/soc_cmap2.png' health = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Health').get() ndvi = health["ndvi"] evi = health["evi"] ndwi = health["ndwi"] ndre = health["ndre"] vari = health["vari"] try: soc = health["soc"] except: soc = nil if lang == 'ar': alignmentType = WD_ALIGN_PARAGRAPH.RIGHT else: alignmentType = WD_ALIGN_PARAGRAPH.LEFT document = Document() font = document.styles['Normal'].font font.name = 'Calibri' header_img_url = 'APIOrganizations/'+uid + '.jpeg' try: header_img = document.add_picture(header_img_url,width=Inches(7.5)) except: header_img = document.add_picture('doc_header.png',width=Inches(7.5)) document.add_heading('Field Report',0) sections = document.sections for s in sections: s.top_margin=Inches(1.0) s.bottom_margin = Inches(0.5) s.left_margin = Inches(0.5) s.right_margin = Inches(0.5) section = document.sections[0] footer = section.footer para = footer.paragraphs[0] p = document.add_paragraph(all_strings[1][lang_num]) p.paragraph_format.alignment = alignmentType p = document.add_paragraph('') p.add_run('Note: ').bold = True p = document.add_paragraph('') p.add_run(all_strings[2][lang_num]).bold = True p.add_run(report_date).italic = True p.paragraph_format.alignment = alignmentType p =document.add_heading(all_strings[3][lang_num],level=1) p.paragraph_format.alignment = alignmentType p = document.add_paragraph('') p.add_run(all_strings[4][lang_num]).bold = True p.add_run(field_address).italic = True p.paragraph_format.alignment = alignmentType p.add_run(all_strings[5][lang_num]).italic = True p.paragraph_format.alignment = alignmentType p = document.add_paragraph('') p.add_run(all_strings[6][lang_num]).bold = True p.paragraph_format.alignment = alignmentType p.add_run(str(field_area)).italic = True p.add_run(all_strings[7][lang_num]).italic = True p.paragraph_format.alignment = alignmentType p = document.add_paragraph('') f_lat = str(field_lat) f_lat = f_lat[0:6] f_long = str(field_long) f_long = f_long[0:6] p.add_run(all_strings[8][lang_num]).bold = True p.paragraph_format.alignment = alignmentType p.add_run(all_strings[9][lang_num]).italic = True p.paragraph_format.alignment = alignmentType p.add_run(f_lat).italic = True p.add_run(all_strings[10][lang_num]).italic = True p.paragraph_format.alignment = alignmentType p.add_run(f_long).italic = True p = document.add_paragraph('') p.add_run(all_strings[11][lang_num]).bold = True p.paragraph_format.alignment = alignmentType p.add_run(sensed_imagedate).italic = True cur_date = imagedate cur_weather_data = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).get() try: min_temp = cur_weather_data["min_temp"] except: min_temp = None if min_temp is None: print('Weather data not available') p = document.add_paragraph('') p.add_run(all_strings[12][lang_num]).bold = True p.paragraph_format.alignment = alignmentType else: min_temp = float(min_temp)-273 min_temp = str(min_temp) min_temp = min_temp[0:5] max_temp = cur_weather_data["max_temp"] max_temp = float(max_temp)-273 max_temp = str(max_temp) max_temp = max_temp[0:5] humidity = cur_weather_data["humidity"] pressure = cur_weather_data["pressure"] station = cur_weather_data["station"]