導航:首頁 > 編程語言 > python修改添加excel

python修改添加excel

發布時間:2022-07-24 07:15:36

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庫可以做到:

  1. 使用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
望採納,謝謝!

閱讀全文

與python修改添加excel相關的資料

熱點內容
python入門迷宮 瀏覽:67
Python打折代碼不含商品 瀏覽:218
把多個Word合成一個pdf 瀏覽:354
aes演算法描述 瀏覽:899
新手機壓縮包在哪 瀏覽:779
java抽獎程序源碼 瀏覽:700
汽車壓縮機又叫 瀏覽:95
android讀取data文件 瀏覽:874
紅旗智聯app怎麼跟h5車子連接 瀏覽:138
材料化學pdf 瀏覽:114
伺服器機房都有什麼東西 瀏覽:370
最近長陰短柱量能副圖指標源碼 瀏覽:647
python字元串去除後四位 瀏覽:167
捷速pdf編輯器破解版 瀏覽:725
大帶寬伺服器怎麼租 瀏覽:299
籃球程序員單身難嗎 瀏覽:877
一接到命令就 瀏覽:488
挖幣伺服器是什麼 瀏覽:524
攜帶型u盤加密 瀏覽:464
apex找不到伺服器怎麼解決 瀏覽:316