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
liuxun349
V2EX  ›  Python

Python 操作 excel 的相关问题

  •  
  •   liuxun349 · 2018-05-09 19:17:06 +08:00 · 3361 次点击
    这是一个创建于 2150 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我对一已存在 excel 表进行读写操作,该表含多 sheet,保存之后,却只有最后一个 sheet。
    怎么才能对多 sheet 操作保存后,能达到想要的效果呢?
    大致写法:
    table = xlrd.open_workbook("test.xls", formatting_info=True)
    ctable = copy(table)
    p_sheet1 = ctable.get_sheet(0)
    p_sheet2 = ctable.get_sheet(1)
    .....对 sheet1,sheet2,读写操作....
    ctable.save("test.xls")
    15 条回复    2018-05-10 19:50:29 +08:00
    myyou
        1
    myyou  
       2018-05-09 19:21:37 +08:00
    保存的时候改名字
    bomb77
        2
    bomb77  
       2018-05-09 19:41:25 +08:00
    xlutils
    liuxun349
        3
    liuxun349  
    OP
       2018-05-09 19:49:53 +08:00 via iPhone
    @myyou 是改 sheet 的名字吗?文件名改是没有作用的
    liuxun349
        4
    liuxun349  
    OP
       2018-05-09 19:50:06 +08:00 via iPhone
    @bomb77 求详解
    golmic
        5
    golmic  
       2018-05-09 19:51:41 +08:00 via Android
    最关键的代码被你省略了。。。
    liuxun349
        6
    liuxun349  
    OP
       2018-05-09 19:53:28 +08:00 via iPhone
    @golmic 啊?我就是使用 p_sheet1.write
    ()
    SingeeKing
        7
    SingeeKing  
       2018-05-09 20:05:14 +08:00 via iPhone
    换成 xlutils

    BUT 为什么不用 openpyxl
    anguslg
        8
    anguslg  
       2018-05-10 00:08:19 +08:00
    看了下文档, ctable 在 save 之前大概要先 add_sheet
    不过 更推荐用 pandas.DataFrame 处理
    catzh
        9
    catzh  
       2018-05-10 09:14:11 +08:00
    同问,为什么不用 openpyxl ?
    myyou
        10
    myyou  
       2018-05-10 10:07:21 +08:00
    from xlutils.copy import copy
    用这个 copy 可以在原有 Excel 修改
    jfry
        11
    jfry  
       2018-05-10 11:40:24 +08:00
    建议用 openpyxl,而且如果需要制作报表的话可以看看 [toaco/tablereport: A python library for making table report.]( https://github.com/toaco/tablereport),完全兼容 openpyxl,哈哈,溜了溜了
    liuxun349
        12
    liuxun349  
    OP
       2018-05-10 15:38:51 +08:00
    @SingeeKing
    @catzh
    因为之前不知道有 openpyxl,为了图快速解决问题,所以选择自己知道的。
    liuxun349
        13
    liuxun349  
    OP
       2018-05-10 15:40:27 +08:00
    @myyou
    就是用的 xlutis.copy
    TGlivecoding
        14
    TGlivecoding  
       2018-05-10 19:37:49 +08:00
    其实更推荐你用 http://xlsxwriter.readthedocs.io/ 现在用.xls 的不多了
    iblislsy
        15
    iblislsy  
       2018-05-10 19:50:29 +08:00
    pandas 在保存 excel 的时候有一个参数 sheet_name, 了解一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1016 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.