使用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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值