Skip to content

Commit 81378ed

Browse files
authored
Merge pull request #361 from Om25091210/excel-to-firebase
Excel to Firebase Realtime Database Integration.
2 parents f3da7e1 + 8ff85d7 commit 81378ed

File tree

8 files changed

+440
-0
lines changed

8 files changed

+440
-0
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!--Please do not remove this part-->
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
4+
5+
# PDF Page Color Counter
6+
7+
## 🛠️ Description
8+
This Python project provides the integration that eliminates the need for manual data entry and facilitates the quick and accurate transfer of data from Excel to Firebase. Push thousands of data from excel to firebase in mins.
9+
10+
**Key Feature :**
11+
12+
* Excel Data Parsing: We will create a feature to parse Excel spreadsheets, extracting structured data to be used in the Firebase Realtime Database. This parsing functionality will support various Excel formats, ensuring compatibility with a wide range of data sources.
13+
14+
15+
## ⚙️ Languages or Frameworks Used
16+
- **Python**: The primary programming language used for the project.
17+
- **Flask**: Flask is a micro web framework for Python that is lightweight and easy to use.
18+
- **Pandas**: Pandas is a popular open-source Python library used for data manipulation and analysis.
19+
20+
## 🌟 How to run
21+
- ### Install all the requirements
22+
- Run `pip install -r requirements.txt` to install all the requirements.
23+
- ### Firebase Setup for Project
24+
25+
- Create a [firebase](https://firebase.google.com/) project, set up a web project and get all the `Project Configurations` from `Project Settings`.
26+
27+
- Navigate to the **Authentication** section in your firebase project and enable the `Email and Password`
28+
authentication.
29+
30+
- The `Project Configurations` will look as follows :-
31+
```bash
32+
"apiKey": YOUR_API_KEY ,
33+
"authDomain": YOUR_AUTH_DOMAIN,
34+
"databaseURL": YOUR_DATABASEURL,
35+
"projectId": YOUR_PROJECT_ID,
36+
"storageBucket": YOUR_STORAGE_BUCKET,
37+
"messagingSenderId": YOUR_MESSAGING_SENDER_ID,
38+
"appId": YOUR_APP_ID,
39+
"measurementId": YOUR_MEASUREMENT_ID
40+
```
41+
- ### Setup Environment for the project
42+
- Now create a `.env` file in your project dreictory and include the following parameters as it is :-
43+
```bash
44+
export FIREBASE_APIKEY=YOUR_API_KEY
45+
export FIREBASE_AUTHDOMAIN=YOUR_AUTH_DOMAIN
46+
export FIREBASE_DATABASEURL=YOUR_DATABASEURL
47+
export FIREBASE_PROJECT_ID=YOUR_PROJECT_ID
48+
export FIREBASE_STORAGE_BUCKET=YOUR_STORAGE_BUCKET
49+
export FIREBASE_MESSAGING_SENDER_ID=YOUR_MESSAGING_SENDER_ID
50+
export FIREBASE_APP_ID=YOUR_APP_ID
51+
export FIREBASE_MEASUREMENT_ID=YOUR_MEASUREMENT_ID
52+
```
53+
54+
- ### Setup a Virtual Enviroment
55+
56+
- Run this command in your terminal `python -m venv myenv`.
57+
- Change your directory by `cd myenv/Scripts` if on windows.
58+
- Activate the virtual enviroment by running this command `source activate`.
59+
- Move out from virtual env to your **Project Directory** by `cd..` .
60+
- Install the packages if not present - `uvicorn`, `Flask`, `pandas`, `numpy`, `openpyxl`, `firebase`.
61+
62+
- ### Now Just, Run the project
63+
64+
-Now Run the following command - `python main.py`.
65+
-You will see output in your terminal indicating that the Flask app is running, usually on http://127.0.0.1:5000/
66+
-Open your web browser and visit the URL specified in the output to access your Flask application.
67+
68+
69+
## 📺 Demo
70+
![image](https://github.com/Om25091210/Python-project-Scripts/assets/74484315/85873602-a1e2-4a1e-acef-8b66a27ae488)
71+
![image](https://github.com/Om25091210/Python-project-Scripts/assets/74484315/d7d7bb3f-eaf0-4963-97e7-f1204539b10f)
72+
73+
74+
75+
## 🤖 Author
76+
77+
Github - [OM YADAV](https://github.com/Om25091210)
78+
LinkedIn - [OM YADAV](www.linkedin.com/in/omyadav)
79+
80+
81+
82+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "your project ID",
4+
"private_key_id": "your private_key_id",
5+
"private_key": "your private_key",
6+
"client_email": "your client email",
7+
"client_id": "your client id",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "client_x509_cert_url"
12+
}
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import firebase_admin
2+
from flask import Flask,render_template,request,redirect
3+
from flask.helpers import url_for
4+
from openpyxl import load_workbook
5+
import pandas as pd
6+
import numpy as np
7+
# from firebase_admin import db
8+
# from firebase_admin import credentials
9+
from datetime import date
10+
import datetime
11+
from firebase import Firebase
12+
import os
13+
14+
firebaseConfig = {
15+
"apiKey": "your apikey",
16+
"authDomain": "",
17+
"databaseURL": "database url",
18+
"projectId": "your project id",
19+
"storageBucket": "your storage bucket id",
20+
"messagingSenderId": "your sender id",
21+
"appId": "your appId",
22+
"measurementId": "your measurement id"
23+
};
24+
firebase = Firebase(firebaseConfig)
25+
db = firebase.database()
26+
list_of_keys=[]
27+
28+
app = Flask(__name__)
29+
30+
@app.route("/",methods = ['GET','POST'])
31+
def start():
32+
if request.method == 'POST':
33+
global file,s_ds1
34+
file = request.files['file']
35+
#file.save("random.xlsx")
36+
file.save("static/police_record_today.xlsx")
37+
global wb
38+
wb = load_workbook(file) # Work Book
39+
# Work Sheet MCRC.RM.COLL
40+
sheets=wb.sheetnames
41+
42+
return render_template("sheetname.html",sheets = sheets)
43+
return render_template("index.html")
44+
45+
@app.route("/sheet-selection",methods = ['GET','POST'])
46+
def sheet_selection():
47+
if request.method == 'POST':
48+
sheet_name_user = request.form.get("sheet_selected")
49+
print(sheet_name_user)
50+
today = date.today()
51+
ds1 = pd.read_excel("static/police_record_today.xlsx", sheet_name_user)
52+
ds2=pd.read_excel("static/police_record_yesterday.xlsx",sheet_name_user)
53+
#appending both data
54+
merged = ds1.append(ds2)
55+
#dropping duplicates
56+
merged = merged.drop_duplicates(keep=False).sort_index()
57+
#converting to string
58+
data=merged.values.tolist()
59+
for i in range(len(data)):
60+
if(str(data[i][10])!="nan"):
61+
fd=datetime.datetime.strptime(str(data[i][10]), '%d.%m.%Y').strftime('%Y.%m.%d').replace(".","")
62+
key=fd+str(data[i][1])+str(data[i][2])+str(data[i][3])+str(data[i][4])+str(data[i][6])+str(data[i][7])+str(data[i][8]);
63+
print(key)
64+
push_key=key.replace(".","").replace(" ","")
65+
if(str(data[i][1])=="nan" and str(data[i][2])=="nan"):
66+
continue
67+
ct=str(data[i][3])
68+
cn=str(data[i][4]).replace(".0","")
69+
name=str(data[i][5])
70+
ca_yr=str(data[i][6]).replace(".0","")
71+
crn=str(data[i][7]).replace(".0","")
72+
cr_yr=str(data[i][8]).replace(".0","")
73+
d_r=str(data[i][9])
74+
rm_date=str(data[i][10])
75+
before=str(data[i][11])
76+
77+
if(str(data[i][3])=="nan"):
78+
ct="None"
79+
if(str(data[i][4])=="nan"):
80+
cn="None"
81+
if(str(data[i][5])=="nan"):
82+
name="None"
83+
if(str(data[i][6])=="nan"):
84+
ca_yr="None"
85+
if(str(data[i][7])=="nan"):
86+
crn="None"
87+
if(str(data[i][8])=="nan"):
88+
cr_yr="None"
89+
if(str(data[i][9])=="nan"):
90+
d_r="None"
91+
if(str(data[i][10])=="nan"):
92+
rm_date="None"
93+
if(str(data[i][11])=="nan"):
94+
before="None"
95+
diction = {
96+
'A':"",
97+
'B':str(data[i][1]),
98+
'C':str(data[i][2]),
99+
'D':ct,
100+
'E':cn,
101+
'F':name,
102+
'G':ca_yr,
103+
'H':crn,
104+
'I':cr_yr,
105+
'J':d_r,
106+
'K':rm_date,
107+
'L':before,
108+
'date':str(today),
109+
'pushkey':push_key,
110+
'type':sheet_name_user.replace(".","_").strip()
111+
}
112+
db.child('data').child(push_key).set(diction)
113+
os.remove("static/police_record_yesterday.xlsx")
114+
os.rename("static/police_record_today.xlsx","static/police_record_yesterday.xlsx")
115+
#s_ds1.to_excel("static/police_record_yesterday.xlsx",index=False)#save today file as tomorrow
116+
117+
118+
return redirect(url_for('start'))
119+
120+
121+
122+
if __name__ == '__main__':
123+
app.run(debug = True)
124+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
const dragArea=document.querySelector('.drag-area');
3+
const dragtext=document.querySelector('.header');
4+
5+
let button =document.querySelector('.button');
6+
let input=document.querySelector('input');
7+
let file;
8+
9+
button.onclick=()=>{
10+
input.click();
11+
};
12+
13+
//wen browse
14+
input.addEventListener('change',function(){
15+
file=this.files[0];
16+
dragArea.classList.add('active');
17+
displayFile();
18+
});
19+
20+
//When file is inside the drag area
21+
dragArea.addEventListener('dragover',(event)=>{
22+
event.preventDefault();
23+
dragtext.textContent="Release to upload"
24+
dragArea.classList.add('active');
25+
//console.log('File is inside the drag area');
26+
});
27+
28+
//When file leaves the drag area
29+
dragArea.addEventListener('dragleave',()=>{
30+
dragtext.textContent="Drag & Drop";
31+
dragArea.classList.remove('active');
32+
//console.log("File left the drag area");
33+
});
34+
35+
//When the file is dropped in the drag area
36+
dragArea.addEventListener('drop',(event)=>{
37+
event.preventDefault();
38+
39+
file=event.dataTransfer.files[0];
40+
displayFile();
41+
//console.log("the file is droped in the drag area");
42+
});
43+
44+
45+
function displayFile(){
46+
let fileType=file.type;
47+
//console.log(file);
48+
if(fileType=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'){
49+
let fileReader=new FileReader();
50+
fileReader.onload=()=>{
51+
let fileURL =fileReader.result;
52+
console.log(fileURL)
53+
};
54+
fileReader.readAsDataURL(file);
55+
console.log(fileType);
56+
}
57+
else if(fileType=='application/vnd.ms-excel'){
58+
let fileReader=new FileReader();
59+
fileReader.onload=()=>{
60+
let fileURL =fileReader.result;
61+
console.log(fileURL)
62+
};
63+
fileReader.readAsDataURL(file);
64+
console.log(fileType);
65+
}
66+
else{
67+
alert('This file type is not supported.');
68+
dragArea.classList.remove('active');
69+
dragtext.textContent="Drag & Drop";
70+
}
71+
}
72+
73+
74+
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500&display=swap');
2+
3+
4+
*{
5+
margin:0;
6+
padding:0;
7+
box-sizing: border-box;
8+
font-family: 'Poppins',sans-serif;
9+
}
10+
11+
body{
12+
display: flex;
13+
align-items: center;
14+
justify-content: center;
15+
min-height: 100vh;
16+
flex-direction: column;
17+
background: #e0eafc;
18+
background: -webkit-linear-gradient(to right,#cfdef3,#e0eafc);
19+
background: linear-gradient(to right,#cfdef3,#e0eafc);
20+
}
21+
22+
.container{
23+
max-width: 650px;
24+
width: 100%;
25+
padding:30px;
26+
background: #fff;
27+
border-radius: 20px;
28+
box-shadow: rgba(149,157,165,0.2) 0px 8px 24px;
29+
}
30+
31+
.drag-area{
32+
height: 400px;
33+
border: 3px dashed #e0eafc;
34+
display: flex;
35+
align-items: center;
36+
justify-content: center;
37+
flex-direction: column;
38+
margin: 10px auto;
39+
}
40+
41+
h3{
42+
margin-bottom: 20px;
43+
font-weight: 500;
44+
}
45+
46+
.drag-area .icon{
47+
font-size: 50px;
48+
color:#1383ff;
49+
}
50+
51+
.drag-area .header{
52+
font-size: 20px;
53+
font-weight: 500;
54+
color: #34495e;
55+
}
56+
57+
.drag-area .support{
58+
font-size: 12px;
59+
color: gray;
60+
margin: 10px 0 15px 0;
61+
}
62+
63+
.drag-area .button{
64+
font-size: 20px;
65+
font-weight: 500;
66+
color: #1683ff;
67+
cursor: pointer;
68+
}
69+
70+
.drag-area.active{
71+
border: 2px solid #1683ff;
72+
}

0 commit comments

Comments
 (0)