使用到的模块链接: http://python-pptx.readthedocs.io/en/latest/
参照的链接: http://www.jianshu.com/p/2ce715671340(简书上的可以用)
代码是三个函数组成 ,其中一个是公共函数 ,上代码 :
#保证数据传输的稳定性的函数,pptx两个函数中用到
def file_iterator(file_name, chunk_size=512):
with open(file_name) as f:
while True:
c = f.read(chunk_size)
if c:
yield c
else:
break
#下载文件 类型
def big_file_download_t(request):
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
prs.save('test23.pptx')
#问题出现在这边 ,没有理解core_properities 这个属性默认是不给title 值的 !!需要自己定义 同时我自己也不能取到 上面 prs.save('test23.pptx')的值 这是两个不一样的东西,所以我必须在这边重新赋值!!
prs.core_properties.title = "test23.pptx"
the_file_name = prs.core_properties.title.encode()
response = StreamingHttpResponse(file_iterator(the_file_name))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
return response
def big_file_download_s(request):
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = 'Adding a Table'
rows = cols = 2
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)
table = shapes.add_table(rows, cols, left, top, width, height).table
# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)
# write column headings
table.cell(0, 0).text = 'Foo'
table.cell(0, 1).text = 'Bar'
# write body cells
table.cell(1, 0).text = 'Baz'
table.cell(1, 1).text = 'Qux'
prs.core_properties.title = "test24.pptx"#确定
the_file_name = prs.core_properties.title.encode()
response = StreamingHttpResponse(file_iterator(the_file_name))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
return response