python把excel变成ppt_用Python写excel和ppt文件

#xlsx和 xls

#一:openpyxl 只能读取xlsx 推荐使用的二种方法

#因为excle文件中可以有多个编排 故可以返回字典

#该方法只适应于xlsx文件,不能处理xls文件

'''

from openpyxl.reader.excel import load_workbook

def readXlsxFile(path):

dic={}

#打开文件

file=load_workbook(filename=path)

#所有表格的名称,返回的是一个列表

#print(file.get_sheet_names())

sheets=file.get_sheet_names()

#拿出一个表格

for sheetName in sheets:

sheet=file.get_sheet_by_name(sheetName)

#一张表所有数据

sheetInfo=[]

#读取数据

for lineNum in range(1,sheet.max_row+1):#行数从1开始

linelist=[]

for columnNum in range(1,sheet.max_column+1):

#拿数据

value=sheet.cell(row=lineNum,column=columnNum).value

#if 根据实际情况

if value!=None:

linelist.append(value)

sheetInfo.append(linelist)

#将一张表的数据存到字典

dic[sheetName]=sheetInfo

sheet=file.get_sheet_by_name(sheets[0])

#最大行数

#print(sheet.max_row)

#最大列数

#print(sheet.max_column)

#表名

#print(sheet.title)

path=r'I:\1.xlsx'

readXlsxFile(path)

'''

#二:读取Xlsx和xls的内容 ,但只能写xls#需要安装,xlrd,future,xlwt-future,pyexcel-io,ordereddict,pyexcel,pyexcel-xls

'''

from collections import OrderedDict #有序字典

from pyexcel_xls import get_data#读取数据

def readXlsAndXlsxFile(path):

dic=OrderedDict()

#抓取数据

xdata=get_data(path) #返回全部表格数据

for sheet in xdata: #xdata的内容可以以sheet表的形式拿出

dic[sheet]=xdata[sheet]

return dic

path=r'I:\1.xlsx'

print(readXlsAndXlsxFile(path))

'''

#三:写入xls文件,只能写如xls,data格式固定

from collections import OrderedDict #有序字典

from pyexcel_xls import save_data#读取数据

def makeExcelFile(path,data):

dic=OrderedDict()

for sheetName,sheetValue in data.items():

d={}

d[sheetName]=sheetValue

dic.update(d)

save_data(path,dic)

path=r'I:\1.xls'

data={'表一':[[1,2,3],[4,5,6],[7,8,9]],'表二':[[11,12,13],[14,15,16],[17,18,19]]}

makeExcelFile(path,data)

import win32com

import win32com.client

def makePPT(path):

ppt=win32com.client.Dispatch('PowerPoint.Application')

ppt.Visible=True

#增加一个文件

pptFile=ppt.Presentations.Add()

#创建一页 Add(参数一,参数二) 参数一为页数(从一开始),参数二为类型(为新建的类型格式)

page1=pptFile.Slides.Add(1,1)

t1=page1.Shapes[0].TextFrame.TextRange #page1.Shapes 获取到ppt的框,Range获得到这个输入的位置

t1.Text='Sunck'

t2=page1.Shapes[1].TextFrame.TextRange #page1.Shapes 获取到ppt的框,Range获得到这个输入的位置

t2.Text='Sunck is good man'

#创建第二页

page2=pptFile.Slides.Add(2,1)

t3=page1.Shapes[0].TextFrame.TextRange #page1.Shapes 获取到ppt的框,Range获得到这个输入的位置

t3.Text='Sunck'

t4=page1.Shapes[1].TextFrame.TextRange #page1.Shapes 获取到ppt的框,Range获得到这个输入的位置

t4.Text='Sunck is good man'

#保存

pptFile.SaveAs(path)

#关闭ppt文件

pptFile.Close()

#关闭ppt的初始化

ppt.Quit()

path=r'I:\1.pptx'

makePPT(path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值