使用django上传接收文档的开发
1.上传csv格式
1)使用python3自带csv库
import csv
# 使用django模块request.FILES。'excel'为上传变量名,解析csv这边选的是GB2312编码
# 在线获取上传文件编码 http://tools.bugscaner.com/filebianma/
upload_file = request.FILES['excel'].read().decode('GB2312')
file_reader = csv.reader(upload_file.split('\n'))
2.上传excel格式
1)安装xlrd库
pip install xlrd
2)django实现
import xlrd
upload_file = request.FILES['excel'].read()
wb = xlrd.open_workbook(filename=None, file_contents=upload_file)
for bom_table in wb.sheets():
num_rows = bom_table.nrows
num_cols = bom_table.ncols
if num_rows == 0 and num_cols == 0:
continue
3.上传pdf格式
1)依赖的库:pdfplumber
这个python库网上都有相对的指导,不太了解的可以去查一下。
pip install
2)django内实现
import pdfplumber
upload_file = request.FILES['excel'].read()
pdf = pdfplumber.open(io.BytesIO(upload_file))
网上查的,使用io.BytesIO(upload_file),我这边使用没有问题
4.上传word格式
1)依赖的库:docx
使用方法:后续补上。
# 查的安装方法使用国内镜像安装更快
pip install python-docx -i https://pypi.douban.com/simple
2)django内实现
import docx
upload_file = request.FILES['excel'].read()
doc = docx.Document(io.BytesIO(upload_file))
for tb in doc.tables:
for row in tb.rows:
for headline in row.cells:
print(headline.text)
一个初学者,有什么错误的地方,请各路大神指导,感谢。
[1]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
[2]: https://mermaidjs.github.io/
[3]: https://mermaidjs.github.io/
[4]: http://adrai.github.io/flowchart.js/