Advertisement
so_hard

csv scrapy pipeline

Nov 4th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. class CsvPipeline:
  2.     def __init__(self):
  3.         date = datetime.today().strftime("%d-%m-%Y")
  4.         self.filename = f'{date}.xlsx'
  5.         self.workbook = openpyxl.Workbook()
  6.  
  7.         # worksheet = workbook.active
  8.         # worksheet.title = "data"
  9.  
  10.     def open_spider(self, spider):
  11.         pass
  12.  
  13.     def close_spider(self, spider):
  14.         self.workbook.save(self.filename)
  15.  
  16.     def process_item(self, item, spider):
  17.         try:
  18.             ws = self.workbook[item['data'][0]]
  19.         except KeyError:
  20.             ws = self.workbook.create_sheet(item['data'][0])
  21.         item_d = list(item['data'][1])
  22.         max_row = ws.max_row
  23.         ws.cell(row=1, column=1, value=item['data'][0])
  24.         for row, entry in enumerate(item_d, start=1):
  25.             ws.cell(row=max_row+row, column=1, value=entry)
  26.         return item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement