超简单:用Python让Excel飞起来,4.07 求解惑

import os
import xlwings as xw
app = xw.App(visible=False, add_book=False)
fp = r'D:\1\销售表1'
fl = os.listdir(fp)
wb = app.books.open(r'D:\1\新增产品表.xlsx')
ws = wb.sheets['新增产品']
vl = ws.range('A1').expland('table')
sc = (2, 1)
ec = (vl.shape[0], vl.shape[1])
ca = ws.range(sc, ec).value
for i in fl:
    if os.path.splitext(i)[1] == '.xlsx':
        try:
            wbs = xw.Book(fp + '\\' + i)
            st = wbs.sheets['产品分类表']
            soe = st.range('A1').expand()
            st.range(soe.shape[0] + 1, 1).value = ca
            wbs.save()
        finally:
            wbs.close()
wb.close()
app.quit()


```为何运行报错,实在是看不懂
求解惑
报错如下:
Traceback (most recent call last):
  File "C:\Users\hp\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-71c983641982>", line 1, in <module>
    runfile('C:/Users/hp/Desktop/Py-Excel/新包/4.07-1.py', wdir='C:/Users/hp/Desktop/Py-Excel/新包')
  File "D:\Pycharm_Professional_2021.2.1_Protable\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\Pycharm_Professional_2021.2.1_Protable\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/hp/Desktop/Py-Excel/新包/4.07-1.py", line 8, in <module>
    vl = ws.range('A1').expland('table')
AttributeError: 'Range' object has no attribute 'expland'

这个错误是说ws.range('A1') 对象没有.expland()方法
应该是expand() 你多写了个 l

vl = ws.range('A1').expand('table')

如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632