3 Star 9 Fork 2

冰封飞飞/计算机英语词频统计

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pdfCreator.py 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
'''
PDF生成模块,需要安装wkhtmltopdf软件,首先生成HTML表格,然后转换为PDF
example:
ubuntu: sudo apt-get install wkhtmltopdf
centos: yum install wkhtmltopdf
'''
import pdfkit
from datastore import datastore
from model.models import modelWordCount
import statistics
import os
class pdfCreator:
PDF_NAME = r'data/wordlist.pdf'
HTML_NAME = r'data/wordlist.html'
def __init__(self):
self.datastore = datastore()
def generateWordList(self, number):
words = self.datastore.getTopNWords(number)
cnt = 1
with open(pdfCreator.HTML_NAME, 'w') as f:
f.write('<html>')
f.write('<head><meta charset="UTF-8"></head>')
f.write('<body>')
f.write('<p>共统计{}单词,Top{}单词表</p>'.format(statistics.getWordCount(), number))
f.write(r'<table border="1">')
f.write('<tr>')
f.write('<th>'+ '序号' + '</th>')
f.write('<th>'+ '词频' + '</th>')
f.write('<th>'+ '单词' + '</th>')
f.write('<th>'+ '释义' + '</th>')
f.write('</tr>')
for word in words:
f.write('<tr>')
f.write('<td>' + 'No.{}'.format(cnt) + '</td>')
f.write('<td>' + str(word.count) + '</td>')
f.write('<td>' + word.word + '</td>')
if word.trans:
f.write('<td>' + word.trans + '</td>')
else:
f.write('<td>N/A</td>')
f.write('</tr>')
cnt += 1
f.write('</table>')
f.write('</body>')
f.write('</html>')
pdfkit.from_file(pdfCreator.HTML_NAME, pdfCreator.PDF_NAME)
os.remove(pdfCreator.HTML_NAME)
if __name__ == '__main__':
a = pdfCreator()
a.generateWordList(5000)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/bingfengfeifei/wordCount.git
git@gitee.com:bingfengfeifei/wordCount.git
bingfengfeifei
wordCount
计算机英语词频统计
master

搜索帮助