In [6]:
from pymongo import MongoClient
import os
import gridfs
import docx
In [7]:
conn = MongoClient("mongodb://sandeep:382pFnNihl0L9HI9@cluster0-shard-00-00-2ivm7.mongodb.net:27017,cluster0-shard-00-01-2ivm7.mongodb.net:27017,cluster0-shard-00-02-2ivm7.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin")
In [8]:
db=conn['test']
In [9]:
conn.database_names() # getting database names
Out[9]:
In [10]:
db.collection_names()
Out[10]:
In [11]:
rrdb = conn['RightResource'] # getting the index
In [12]:
rrcol=rrdb['resources'] # getting the collection from the index
In [13]:
rrcol.insert_one({_id:1,name:"sandeep"})
In [ ]:
rrcol.find("{_id:1}")
In [ ]:
fs = gridfs.GridFS(rrdb)
In [ ]:
#a=fs.put(b"hello world")
In [ ]:
doc = docx.Document('C:/Users/sandeep/work/sample.docx')
In [ ]:
doc.save('C:/Users/sandeep/work/sampled.docx')
In [ ]:
filename="C:/Users/sandeep/work/sampled.docx"
fileext=str(os.path.splitext(filename)[1]).lower()
filetype = ""
def formatswitcher(fileext):
if(fileext == ".docx"):
filetype="application/docx"
elif(fileext is ".pdf"):
filetype="application/pdf"
elif(fileext is ".doc"):
filetype="application/doc"
elif(fileext is ".jpeg"):
filetype="jpeg"
elif(fileext is ".png"):
filetype="png"
else:
filetype="invalid"
return filetype
In [ ]:
filebuffer = open(filename,"rb")
In [ ]:
def insert(cid,filetype,filebuffer):
if(fs.exists(_id=cid)==True):
print("file already exist updating the document with latest information")
fs.delete(cid)
fs.put(filebuffer,_id=cid, ContentType=filetype)
else:
fs.put(filebuffer,_id=cid, ContentType=filetype)
In [ ]:
def receive(cid,outpath):
rawfile = open("C:/Users/sandeep/work/sampleds.docx",'wb')
In [ ]:
insert(1,filetype,filebuffer)
In [ ]:
In [ ]:
x= fs.get(1)
In [ ]:
print(x.read())
In [ ]:
x.read
In [ ]:
b = open("C:/Users/sandeep/work/sampleds.docx",'wb')
In [ ]:
a=open("C:/Users/sandeep/work/sampled.docx",'rb').read()
In [ ]:
y = fs.put(a,_id=2,name="sandeep",filename="resume",ContentType="application/docx")
In [ ]:
ss=fs.get(y).read()
In [ ]:
b.write(x.read())
b.close()
In [ ]:
lst =list(rrdb.fs.files.find())
In [ ]:
rr = open(r"C:/Users/sandeep/work/sample.docx",'rb').read()
In [ ]:
xx=fs.put(rr,ContentType="application/docx")
In [ ]:
fs.get(xx).read()
In [ ]:
fs.get(xx).read()
In [ ]:
lst
In [ ]:
y
In [ ]:
fs.files.find(_id=1)
In [ ]: