win32com.client操作excel问题

yesong_zhang 2018-04-03 01:59:43
我有一个类文件,文件名保存为myeasyexcel.py

from win32com.client import Dispatch
import win32com.client
class EasyExcel:
"""A utility to make it easier to get at Excel. Remembering
to save the data is your problem, as is error handling.
Operates on one workbook at a time."""
def __init__(self, filename=None): #打开文件或者新建workbook(如果不存在的话)
self.xlApp = win32com.client.Dispatch('Excel.Application')

if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''

然后在另外一个py文件中引入这个类文件,但是执行后最后提示ws=wb.Worksheets(selected_name)中Worksheets方法不存在,AttributeError: 'EasyExcel' object has no attribute 'Worksheets'
Worksheets是win32com.client中的方法,怎么就不支持了?

def deleteROW():
import myeasyexcel
wb=myeasyexcel.EasyExcel(filename=os.path.join(os.getcwd(),'test.xlsx'))
ws=wb.Worksheets(selected_name)

max_row=ws.UsedRange.Rows.Count
max_col=ws.UsedRange.Column.Count

print(max_row,max_col)
...全文
1242 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈年椰子 2018-04-03
  • 打赏
  • 举报
回复
或者直接把 self.xlBook 作为公有成员使用。
陈年椰子 2018-04-03
  • 打赏
  • 举报
回复
没用过win32com 操作excel 按你的代码,class EasyExcel下,要增加Worksheets 类似这样

def Worksheets(selected_name):
    self.xlSheet = win32com.client.Worksheets(selected_name)
    ...
或者直接把

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧