-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLdata_to_report.py
55 lines (40 loc) · 1.9 KB
/
SQLdata_to_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pandas as pd
import numpy as np
import mysql.connector
from mysql.connector import Error
from docxtpl import DocxTemplate
# Set up connection to MySQL database
try:
connection = mysql.connector.connect(
host="qntm.mysql.database.azure.com",
user="harrison",
password="Welc0meT0Q!!",
database="testdb"
)
print("Connection established successfully.")
except mysql.connector.Error as error:
print("Error: Unable to connect to MySQL database.", error)
# Load data into pandas dataframes and convert to array
agg_df = pd.read_sql_query('SELECT * FROM aggregated_data', connection)
cont_df = pd.read_sql_query('SELECT * FROM contacts', connection)
# Merge data frames with all data
merged_df = agg_df.merge(cont_df, on='Company', how='outer')
merged_df = merged_df.fillna('')
# Get a list of the unique company names
company_names = merged_df['Company'].unique()
# Load the template file
template_path = r'C:\Users\New User\Segmentation\Template\template.docx'
template = DocxTemplate(template_path)
#Creates a report based on the template
#for i, company_name in enumerate(company_names[0:2]): #for companies between i an j
for company_name in company_names: #for all companies in the database
# Filter the dataframe for the current company
company_df = merged_df[merged_df['Company'] == company_name]
# Convert the company dataframe to a dictionary of records
context = company_df.to_dict('records')
# Render the template with the current company's data
template.render(context[0])
# Define the file path and name to save the generated document
file_path = r'C:\Users\New User\Segmentation\Reports\{}_{}.docx'.format(company_name, 'report')
# Save the generated document to the new file with the company name and index as the file name
template.save(file_path)