xlwings分拆excel表格提示AttributeError: 'str' object has no attribute 'expand'
下面是我的代码和excel表格,这种情况要怎么解决?
import xlwings as xw
df = pd.read_excel("C:\\Users\\Link\\Desktop\\python数据分析期末报告\\xiugai.xlsx")
df["交易日期"] = pd.to_datetime(df["交易日期"])
df["年"]=df["交易日期"].dt.year
resultPath = 'C:\\Users\\Link\\Desktop\\python数据分析期末报告\\x.xlsx'
df.to_excel(resultPath,index = False)
file_path = 'C:\\Users\\Link\\Desktop\\python数据分析期末报告\\x.xlsx'
sheet_name = 'sheet1'
app = xw.App(visible=False,add_book=False)
workbook = app.books.open(file_path)
worksheet = workbook.sheets[sheet_name]
value = worksheet.range('A1').expand('年').value
data = dict()
for i in range(len(value)):
product_name = value[i][12]
if product_name not in data:
data[product_name] = []
data[product_name].append(value[i])
for key,value in data.items():
new_workbook = xw.books.add()
new_worksheet = new_workbook.sheets.add(key)
new_worksheet['A1'].value = worksheet['A1:H1'].value
new_worksheet['A2'].value = value
new_workbook.save('{}.xlsx'.format(key))
app.quit()
AttributeError Traceback (most recent call last)
Input In [21], in <cell line: 8>()
6 workbook = app.books.open(file_path)
7 worksheet = workbook.sheets[sheet_name]
----> 8 value = worksheet.range('A1').expand('年').value
9 data = dict()
10 for i in range(len(value)):
File E:\Anaconda3\lib\site-packages\xlwings\main.py:2025, in Range.expand(self, mode)
1999 def expand(self, mode='table'):
2000 """
2001 Expands the range according to the mode provided. Ignores empty top-left cells (unlike ``Range.end()``).
2002
(...)
2023 .. versionadded:: 0.9.0
2024 """
-> 2025 return expansion.expanders.get(mode, mode).expand(self)
AttributeError: 'str' object has no attribute 'expand'