-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcelWriter.py
88 lines (74 loc) · 2.91 KB
/
excelWriter.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pandas as pd
import pymongo
from db_operations import filterWithUrl
import os
from io import BytesIO
from urllib.request import urlopen
from UliPlot.XLSX import auto_adjust_xlsx_column_width
from collections import OrderedDict
noOfEntriesShown=800 #change this value to get more entries in excel sheets corresponding to each product
#authorization
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
database = myclient["amazonItems"]
itemsCollection = database["items"]
def generateXL(asinList):
try:
filename="report.xlsx"
writer=pd.ExcelWriter(filename)
try:
os.remove(filename)
except:
pass
writer.book.formats[0].set_text_wrap()
#making sheet "Home"
homeData={}
sheetname="Home"
i=2
for asin in asinList:
url="https://www.amazon.in/dp/"+asin.replace(" ","").replace("\n","")
data = filterWithUrl(url,itemsCollection)
latestDate=data["latest"]
latestData=data[latestDate]
homeReport={
"ASIN":latestData.get("ASIN"),
"Title":latestData.get("title"),
"Image":latestData.get("image"),
"Price":latestData.get("price"),
"Availability":latestData.get("availability"),
"Rating":latestData.get("rating"),
"About":latestData.get("about"),
"Best Sellers Rank":latestData.get("Best Sellers Rank")
}
homeData[asin]=homeReport
df = pd.DataFrame(homeData).T
df.to_excel(writer,sheet_name=sheetname,index=False)
auto_adjust_xlsx_column_width(df, writer, sheet_name=sheetname, margin=10)
#making sheets of individual products
#adding images to sheet
i=2
for asin in homeData.keys():
try:
img_url=homeData[asin]["Image"]
image_data = BytesIO(urlopen(img_url).read())
worksheet = writer.sheets["Home"]
worksheet.insert_image('C'+str(i), img_url,{'image_data': image_data,'x_scale': 0.25, 'y_scale': 0.25})
except:
pass
i+=1
#adding data to sheet
for asin in asinList:
sheetname=asin
url="https://www.amazon.in/dp/"+asin.replace(" ","").replace("\n","")
data = filterWithUrl(url,itemsCollection)
del data["latest"]
del data["asin"]
del data["url"]
del data["_id"]
global noOfEntriesShown
data = OrderedDict(reversed(list(data.items())[:noOfEntriesShown]))
df = pd.DataFrame(data).T
df.to_excel(writer,sheet_name=sheetname,index=True)
auto_adjust_xlsx_column_width(df, writer, sheet_name=sheetname, margin=0)
writer.save()
except Exception as e:
print(e)