V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Hyanide
V2EX  ›  Python

如上代码发现发生意外,已经试过很多方法,比如修改注册路径、重装等,仍然无法解决。新手,求大神指点! 不知道是代码写的问题,还是系统接口调用问题,怀疑自己,跪谢大神。

  •  
  •   Hyanide · 2020-06-01 13:19:08 +08:00 · 2683 次点击
    这是一个创建于 1423 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import os
    import xlwings as xw

    files_path = r'C:/Users/Peter Pro/Desktop/zh'

    app = xw.App(visible=True, add_book=False)
    app.display_alerts = False
    app.screen_updating = False

    A=['D14','E14','F14','G14','H14','I14','J14','K14','L14','M14']
    B=['D15','E15','F15','G15','H15','I15','J15','K15','L15','M15']
    C=['D13','E13','F13','G13','H13','I13','J13','K13','L13','M13']
    D=['C15','D15','E15','F15','G15','H15','I15','J15','K15','L15']

    for files_path_0, dirs, files in os.walk(files_path):
    for file in files:
    wb = app.books.open(files_path+'/'+file)
    sht = wb.sheets['测算表']
    for i in range(9):
    sht.range(A[i]).formula = '='+C[i]+'/1.01'
    sht.range(B[i]).formula = '=IF('+A[i]+'<=100000,'+A[i]+'*1.01,IF('+A[i]+'>100000,'+A[i]+'))+'+D[i]
    wb.save()
    wb.close()
    app.quit()
    app.kill()
    4 条回复    2020-06-01 16:09:58 +08:00
    Hyanide
        1
    Hyanide  
    OP
       2020-06-01 13:32:30 +08:00
    补充一下错误反馈:
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
    File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
    File "C:/Users/hyani/Documents/Masterpiece/demo/zh.py", line 21, in <module>
    sht.range(A[i]).formula = '='+C[i]+'/1.01'
    File "C:\Users\hyani\AppData\Local\Programs\Python\Python38\venv\lib\site-packages\xlwings\main.py", line 842, in range
    return Range(impl=self.impl.range(cell1, cell2))
    File "C:\Users\hyani\AppData\Local\Programs\Python\Python38\venv\lib\site-packages\xlwings\_xlwindows.py", line 610, in range
    xl1 = self.xl.Range(arg1)
    File "C:\Users\hyani\AppData\Local\Programs\Python\Python38\venv\lib\site-packages\xlwings\_xlwindows.py", line 66, in __call__
    v = self.__method(*args, **kwargs)
    File "<COMObject <unknown>>", line 2, in Range
    pywintypes.com_error: (-2147023170, '远程过程调用失败。', None, None)
    Hyanide
        3
    Hyanide  
    OP
       2020-06-01 16:05:37 +08:00
    @Latin 大神,按照链接中帖子,添加了下方语句,还是失败

    添加了帖子中的方法:
    import win32api
    win32api.FormatMessage(-2147352565)
    'Invalid index.\r\n'
    Hyanide
        4
    Hyanide  
    OP
       2020-06-01 16:09:58 +08:00
    @Latin 而且也用了 os.path.abspath(rel_path),这个路径
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1364 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:41 · PVG 07:41 · LAX 16:41 · JFK 19:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.