MongoDB - 將查詢結果保存到excel文件中


import pymongo
import re

client = pymongo.MongoClient('127.0.0.1', 27017)
db_name = 'Trade'

db = client[db_name]


filter1 = {'$or':[{"phone": {'$ne':""}}, {"cell_phone": {'$ne':""}}]}
filter2 = {'address':1, 'contact':1, 'phone':1, 'cell_phone':1, '_id':0}

results = db.business.find(filter1, filter2)

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

ws.append(('地址', '聯系人', '電話', '手機'))
# for _ in a:
#     print(a.next())

for data in results:
    address = data['address']
    contact = data['contact']
    phone = data['phone']
    cell_phone = data['cell_phone']
    result = (address, contact, phone, cell_phone)
    ws.append(result)

wb.save("sample.xlsx")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM