add excel function
This commit is contained in:
parent
49e5ed514f
commit
6de4bb026d
|
@ -0,0 +1,88 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
"""
|
||||
@project: PyCharm
|
||||
@file: excel.py
|
||||
@author: Shengqiang Zhang
|
||||
@time: 2020/4/11 21:14
|
||||
@mail: sqzhang77@gmail.com
|
||||
"""
|
||||
|
||||
|
||||
import xlrd
|
||||
import xlwt
|
||||
from xlutils.copy import copy
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('hello world!')
|
||||
|
||||
|
||||
def write_excel_xls(path, sheet_name_list, value):
|
||||
|
||||
# 新建一个工作簿
|
||||
workbook = xlwt.Workbook()
|
||||
|
||||
# 获取需要写入数据的行数
|
||||
index = len(value)
|
||||
|
||||
for sheet_name in sheet_name_list:
|
||||
|
||||
# 在工作簿中新建一个表格
|
||||
sheet = workbook.add_sheet(sheet_name)
|
||||
|
||||
# 往这个工作簿的表格中写入数据
|
||||
for i in range(0, index):
|
||||
for j in range(0, len(value[i])):
|
||||
sheet.write(i, j, value[i][j])
|
||||
|
||||
# 保存工作簿
|
||||
workbook.save(path)
|
||||
|
||||
|
||||
|
||||
def write_excel_xls_append(path, sheet_name, value):
|
||||
index = len(value) # 获取需要写入数据的行数
|
||||
workbook = xlrd.open_workbook(path) # 打开工作簿
|
||||
worksheet = workbook.sheet_by_name(sheet_name) # 获取工作簿中所有表格中的的第一个表格
|
||||
rows_old = worksheet.nrows # 获取表格中已存在的数据的行数
|
||||
new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象
|
||||
new_worksheet = new_workbook.get_sheet(sheet_name) # 获取转化后工作簿中的第一个表格
|
||||
for i in range(0, index):
|
||||
for j in range(0, len(value[i])):
|
||||
new_worksheet.write(i + rows_old, j, value[i][j]) # 追加写入数据,注意是从i+rows_old行开始写入
|
||||
new_workbook.save(path) # 保存工作簿
|
||||
print("xls格式表格【追加】写入数据成功!")
|
||||
|
||||
|
||||
def read_excel_xls(path):
|
||||
workbook = xlrd.open_workbook(path) # 打开工作簿
|
||||
sheets = workbook.sheet_names() # 获取工作簿中的所有表格
|
||||
worksheet = workbook.sheet_by_name(sheets[0]) # 获取工作簿中所有表格中的的第一个表格
|
||||
for i in range(0, worksheet.nrows):
|
||||
for j in range(0, worksheet.ncols):
|
||||
print(worksheet.cell_value(i, j), "\t", end="") # 逐行逐列读取数据
|
||||
print()
|
||||
|
||||
|
||||
book_name_xls = '我的书架.xls'
|
||||
|
||||
sheet_name_finish_read = '所有的书籍'
|
||||
sheet_name_recent_read = '最近阅读的书籍'
|
||||
sheet_name_all = '已读完的书籍'
|
||||
|
||||
value_title = [["ID", "标题", "作者", "封面", "简介", "所属目录"], ]
|
||||
|
||||
value1 = [["张三", "男", "19", "杭州", "研发工程师", "1"],
|
||||
["李四", "男", "22", "北京", "医生", "1"],
|
||||
["王五", "女", "33", "珠海", "出租车司机", "1"], ]
|
||||
|
||||
value2 = [["Tom", "男", "21", "西安", "测试工程师", "1"],
|
||||
["Jones", "女", "34", "上海", "产品经理", "1"],
|
||||
["Cat", "女", "56", "上海", "教师", "1"], ]
|
||||
|
||||
write_excel_xls(book_name_xls, [sheet_name_finish_read, sheet_name_recent_read, sheet_name_all], value_title)
|
||||
|
||||
write_excel_xls_append(book_name_xls, sheet_name_finish_read, value1)
|
||||
write_excel_xls_append(book_name_xls, sheet_name_recent_read, value2)
|
||||
#read_excel_xls(book_name_xls)
|
|
@ -10,6 +10,7 @@
|
|||
"""
|
||||
|
||||
from wereader import *
|
||||
from excel import *
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QMainWindow
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
@ -127,6 +128,23 @@ if __name__=='__main__':
|
|||
|
||||
|
||||
|
||||
print(get_bookshelf(USER_VID, HEADERS))
|
||||
print(get_bookmarklist(29845865, HEADERS))
|
||||
#29845865
|
||||
books = get_bookshelf(USER_VID, HEADERS) # 获取书架上的书籍
|
||||
books_finish_read = books['finishReadBooks']
|
||||
#books_finish_read =
|
||||
print(books_finish_read)
|
||||
exit()
|
||||
books_recent_read = books['recentBooks']
|
||||
books_all = books['allBooks']
|
||||
|
||||
|
||||
write_excel_xls('我的书架.xls', ['已读完的书籍', '最近阅读的书籍', '所有的书籍'], value_title) # 写入excel文件
|
||||
write_excel_xls_append('我的书架.xls', '已读完的书籍', '') # 追加写入excel文件
|
||||
|
||||
for type_books in books.keys():
|
||||
for book in books[type_books]:
|
||||
print(book)
|
||||
# print(get_bookmarklist(book.bookId, HEADERS))
|
||||
|
||||
print('\n\n')
|
||||
#29845865
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
requests
|
||||
clipboard
|
||||
xlutils
|
||||
xlrd
|
||||
xlwt
|
||||
PyQt5==5.13.0
|
||||
PyQtWebEngine==5.13.0
|
|
@ -9,6 +9,12 @@
|
|||
@mail: sqzhang77@gmail.com
|
||||
"""
|
||||
|
||||
"""
|
||||
@origin: https://github.com/arry-lee/wereader
|
||||
@author: arry-lee
|
||||
@annotation: modified from arry-lee
|
||||
"""
|
||||
|
||||
from collections import namedtuple, defaultdict
|
||||
from operator import itemgetter
|
||||
from itertools import chain
|
||||
|
@ -20,8 +26,10 @@ import urllib3
|
|||
# 禁用安全警告
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
# 书籍信息
|
||||
Book = namedtuple('Book', ['bookId', 'title', 'author', 'cover', 'intro', 'category'])
|
||||
|
||||
|
||||
Book = namedtuple('Book', ['bookId', 'title', 'author', 'cover', 'category'])
|
||||
|
||||
def get_bookmarklist(bookId, headers):
|
||||
"""获取某本书的笔记返回md文本"""
|
||||
|
@ -136,16 +144,34 @@ def get_bookshelf(userVid, headers):
|
|||
data = r.json()
|
||||
else:
|
||||
raise Exception(r.text)
|
||||
books = set()
|
||||
for book in chain(data['finishReadBooks'], data['recentBooks']):
|
||||
|
||||
books_finish_read = set() # 已读完的书籍
|
||||
books_recent_read = set() # 最近阅读的书籍
|
||||
books_all = set() # 书架上的所有书籍
|
||||
|
||||
|
||||
for book in data['finishReadBooks']:
|
||||
if not book['bookId'].isdigit(): # 过滤公众号
|
||||
continue
|
||||
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['category'])
|
||||
books.add(b)
|
||||
books = list(books)
|
||||
books.sort(key=itemgetter(-1))
|
||||
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
|
||||
books_finish_read.add(b)
|
||||
books_finish_read = list(books_finish_read)
|
||||
books_finish_read.sort(key=itemgetter(-1)) # operator.itemgetter(-1)指的是获取对象的最后一个域的值,即以category进行排序
|
||||
|
||||
return books
|
||||
|
||||
|
||||
for book in data['recentBooks']:
|
||||
if not book['bookId'].isdigit(): # 过滤公众号
|
||||
continue
|
||||
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
|
||||
books_recent_read.add(b)
|
||||
books_recent_read = list(books_recent_read)
|
||||
books_recent_read.sort(key=itemgetter(-1)) # operator.itemgetter(-1)指的是获取对象的最后一个域的值,即以category进行排序
|
||||
|
||||
|
||||
books_all = books_finish_read + books_recent_read
|
||||
|
||||
return dict(finishReadBooks=books_finish_read, recentBooks=books_recent_read, allBooks=books_all)
|
||||
|
||||
|
||||
def get_notebooklist(headers):
|
||||
|
@ -160,8 +186,7 @@ def get_notebooklist(headers):
|
|||
books = []
|
||||
for b in data['books']:
|
||||
book = b['book']
|
||||
b = Book(book['bookId'], book['title'], book['author'],
|
||||
book['cover'], book['category'])
|
||||
b = Book(book['bookId'], book['title'], book['author'], book['cover'], book['intro'], book['category'])
|
||||
books.append(b)
|
||||
books.sort(key=itemgetter(-1))
|
||||
return books
|
||||
|
|
Loading…
Reference in New Issue