pywin32處理表格

進過一些字符串處理,將一行中兩個單元格內容拼接,放在其他單元格中

import win32com.client

goal_file = 'D:/network/python/test/xxxxxxxxxxxxxxxxxxx.xlsx'

def writeExcelWin32(targetFile):
#創建excel
xlsApp = win32com.client.Dispatch("Excel.Application")
#打開excel文件
xlsBook = xlsApp.Workbooks.Open(targetFile)
#打開sheet
for i in range(13): #表格的數量,遍歷需要用的表
i = i + 2 #從第二的表格開始處理
str1 = '正在處理第' + str(i) +'個表格!'
print(str1) #打印正在處理哪個表格
xlsSheet = xlsBook.Sheets(i) #進入工作簿中的第i個表格
info = xlsSheet.UsedRange #查看錶格使用情況
lieshu = info.Rows.Count
hangshu = info.Columns.Count
str2 = '這個表格共'+ str(lieshu)+'行'+str(hangshu)+ '列'
print(str2)
for j in range(lieshu - 2):
j = j + 3
print('正在處理第' +str(j) + '行')
ip2 = str(xlsSheet.Cells(j,6).Value).split('.')[1]
ip3 = str(xlsSheet.Cells(j,6).Value).split('.')[2]
if(len(ip3) == 1):
ip3 = '00' + ip3
elif(len(ip3) == 2):
ip3 = '0' + ip3
else:
ip3 = ip3
xlsSheet.Cells(j,8).Value = xlsSheet.Cells(j,4).Value + ip2 + ip3 #字符轉處理
xlsBook.Save()
xlsBook.Close()
xlsApp.Quit()

if name =="main":
writeExcelWin32(goal_file)

win32com python 查詢excel行數列數

https://blog.csdn.net/chang1976272446/article/details/84141272

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章