⑴ python能修改已存在的excel文件么用
用Excel创建的文件和用openpyxl创建的表格文件应该是不同的,.xls这类文件非文本文件都有“外壳”。openpyxl 相当于创建了破解版的.xls文件。需要注意的是openpyxl针对的是Office 2010 版的Excel,因此有必要看看自己的Excel版本(或者Excel创建的那个文件是否兼容到2010或者2007版),可以到openpyxl的仓库向开发者提issue寻求解决方法。参考:http://bitbucket.org/openpyxl/openpyxl/issues
⑵ python 如何能对一个excel文件进行编辑
你需要win32api模块
#-*-coding:gbk-*-
importwin32com.client
classeasyExcel:
""".Remembering
tosavethedataisyourproblem,asiserrorhandling.
Operatesononeworkbookatatime."""
def__init__(self,filename=None):
self.xlApp=win32com.client.Dispatch('Excel.Application')
iffilename:
self.filename=filename
self.xlBook=self.xlApp.Workbooks.Open(filename)
else:
self.xlBook=self.xlApp.Workbooks.Add()
self.filename=''
defsave(self,newfilename=None):
ifnewfilename:
self.filename=newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
defclose(self):
self.xlBook.Close(SaveChanges=0)
delself.xlApp
defgetCell(self,sheet,row,col):
"Getvalueofonecell"
sht=self.xlBook.Worksheets(sheet)
returnsht.Cells(row,col).Value
defsetCell(self,sheet,row,col,value):
"setvalueofonecell"
sht=self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Value=value
defgetRange(self,sheet,row1,col1,row2,col2):
"returna2darray(i.e.tupleoftuples)"
sht=self.xlBook.Worksheets(sheet)
returnsht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Value
defsetRange(self,sheet,leftCol,topRow,data):
""".
"""
bottomRow=topRow+len(data)-1
rightCol=leftCol+len(data[0])-1
sht=self.xlBook.Worksheets(sheet)
sht.Range(
sht.Cells(topRow,leftCol),
sht.Cells(bottomRow,rightCol)
).Value=data
defgetContiguousRange(self,sheet,row,col):
"""
encountersblankcells;returnsthenon-blankrange.
Looksatfirstrowandcolumn;blanksatbottomorright
"""
sht=self.xlBook.Worksheets(sheet)
#findthebottomrow
bottom=row
whilesht.Cells(bottom+1,col).Valuenotin[None,'']:
bottom=bottom+1
#rightcolumn
right=col
whilesht.Cells(row,right+1).Valuenotin[None,'']:
right=right+1
returnsht.Range(sht.Cells(row,col),sht.Cells(bottom,right)).Value
deffixStringsAndDates(self,aMatrix):
#convertsallunicodestringsands
newmatrix=[]
forrowinaMatrix:
newrow=[]
forcellinrow:
iftype(cell)isUnicodeType:
newrow.append(str(cell))
eliftype(cell)isTimeType:
newrow.append(int(cell))
else:
newrow.append(cell)
newmatrix.append(tuple(newrow))
returnnewmatrix#下面将桌面文件test.xlsx的Worksheets(2)写上一些数据,再把这些数据转置粘贴到Worksheets(1)
if__name__=="__main__":
try:
xls=easyExcel(r'C:Users...Desktop est.xlsx')
st=xls.xlBook.Worksheets(1)
st2=xls.xlBook.Worksheets(2)
st2.Range("A1:G2").Value=900
st2.Range("A1:G2").Copy()
st.Range("A1:B7").Value=0
st.Range("A1").PasteSpecial(Transpose=True,SkipBlanks=True,)
finally:
xls.save()
xls.close()
⑶ 用Python修改excel中一列数据
可以使用pandas库,先读取excel文件,然后使用pandas库来修改一列,然后写入文件即可。
⑷ python修改xls文件
你说的源文件指的是什么?从图中看“源文件”中对excel的写入没有分列,把数据都写在了第一列,这些数据是从哪来的,以什么形式获取的呢?
我按照你的图片写了下面这个代码,是python打开excel对指定单元格写入数据的基本程序,你可以参考参考,希望对你能有所帮助!
==================================
#本程序在python2.6版本下测试通过
import win32com.client #这里用到win32com.client,需要安装pywin32模块
#下载地址 http://sourceforge.net/projects/pywin32/files/pywin32/Build216/
xlApp = win32com.client.Dispatch('Excel.Application') #打开EXCEL,这里不需改动
xlBook = xlApp.Workbooks.Open('D:\\1.xls') #将D:\\1.xls改为要处理的excel文件路径
xlSht = xlBook.Worksheets('sheet1') #要处理的excel页,默认第一页是‘sheet1’
a=['aa','cc','ss','ee','11','qq','sss','fff'] #准备填入excel的数据
b=['aaa','www','rras','ff','gfaga','gg','aa'] #准备填入excel的数据
for i in range(1,len(a)+1):
xlSht.Cells(1,i).Value=a[i-1] #将列表a中数据填入第一行
for i in range(1,len(b)+1):
xlSht.Cells(2,i).Value=b[i-1] #将列表b中数据填入第二行
xlSht.Cells(5,3).Value='abcde' #可以用这种方法给
xlBook.Close(SaveChanges=1) #完成 关闭保存文件
del xlApp
⑸ python有没有可以修改已存在excel文件的模块
xlutils修改2003版,格式会丢失,还没有找到解决办法。
openpyxl修改2007版。
⑹ python怎么把数据写入到excel
Python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取excel是不能对其进行操作的:xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作。而xlwt.Workbook()返回的xlwt.Workbook类型的save(filepath)方法可以保存excel文件。
因此对于读取和生成Excel文件都非常容易处理,但是对于已经存在的Excel文件进行修改就比较麻烦了。不过,还有一个xlutils(依赖于xlrd和xlwt)提供复制excel文件内容和修改文件的功能。其实际也只是在xlrd.Book和xlwt.Workbook之间建立了一个管道而已。
xlutils.模块的()方法实现了这个功能,示例代码如下:
⑺ 用python更改了excle的数据,怎样保存
使用openpyxl库可以做到:
使用pip安装读写excel依赖的openpyxl库:
pipinstallopenpyxl
2. 修改test.xlsx默认工作表的A1单元格并保存的示例代码:
fromopenpyxlimport*
filename='test.xlsx'
wb=load_workbook(filename)
ws=wb.active
ws['A1']='修改的内容'
wb.save(filename)#保存内容
⑻ python修改 excel文件
检查一下xlrd,xlwt,xlutils的版本,xlutil依赖于xlrd和xlwt.
⑼ python有没有可以修改已存在excel文件的模块
这种问题,推荐修改成:
你用过的EXCEL操作包最好用的是哪个?for python 2.x or 3.x
我用过了XXXX,YYYY,ZZZZ,觉得有HHHH个不足的地方。
想必会有各种大牛来回答你。
⑽ python pandas可以修改Excel表格中的某个值的颜色吗
打开已有的excel文件,然后在文件最后写入,添加新数据的函数的。
只不过,可以利用:Working with Excel Files in Python中的库,组合实现。 writing to existing workbook using xlwt
望采纳,谢谢!