V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
a132811
V2EX  ›  分享创造

一个将 xlsx/xls/csv 转成 csv 或者 json 的工具——xlparser

  •  
  •   a132811 ·
    ahuigo · 2018-12-20 15:37:31 +08:00 · 2926 次点击
    这是一个创建于 1952 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Office 的 Excel 以及 MacOs Numbers 转 CSV 总有乱码、丢数据的问题。 所以写了这个命令脚本 xlparser 做 excel2csv 转换。

    希望对大家有用。

    Repo

    Install

    pip3 install xlparser
    pip3 install xcut
    

    Usage

    $ xlparser -h
    Usage: xlparser FILE [options]
    
        # Convert xlsx to csv
        $ xlparser source.xlsx  > new.csv
    
        # Convert csv to csv
        $ xlparser source.csv  > new.csv
    
        # Convert csv to json
        $ xlparser source.csv -json > new.json
    
        options:
           -h       For help.
           -csv     Export to csv(by default).
           -json    Export to json.
    

    Examples

        $ xlparser src.xlsx | tee test.csv
        name, score
        "李雷,韩梅",15
        小花,16
    
        $ xlparser src.xlsx | xcut -f name
        name
        "李雷,韩梅"
        小花
        $ xlparser src.xlsx | xcut -f score,name
         score,name
        15,"李雷,韩梅"
        16,小花
    
    
    8 条回复    2019-01-18 19:10:33 +08:00
    pmyile
        1
    pmyile  
       2018-12-20 23:52:43 +08:00
    xlsx 的不同 sheet 页可否转呢?
    mingyun
        2
    mingyun  
       2018-12-20 23:54:46 +08:00
    λ pip install xcut
    Collecting xcut
    Could not find a version that satisfies the requirement xcut (from versions: )
    No matching distribution found for xcut
    a132811
        3
    a132811  
    OP
       2018-12-21 11:06:48 +08:00
    @pmyile 默认只转第一个 sheet。我周末改一下,把它全转了吧
    a132811
        4
    a132811  
    OP
       2018-12-21 11:10:02 +08:00
    @mingyun 更新下你的 pip,再看看你的源能不能搜得到

    ```
    $ pip install --upgrade pip
    .....

    $ pip search xcut
    xcut (0.0.10) - Xcut is an Enhanced cut command, which is used to help grep column.
    ```
    a132811
        5
    a132811  
    OP
       2018-12-21 11:17:07 +08:00
    @mingyun 你用的什么源?你也可以使用清华的源
    https://mirror.tuna.tsinghua.edu.cn/help/pypi/
    jindaodama
        6
    jindaodama  
       2018-12-21 16:08:24 +08:00
    推荐用 ssconvert,支持多 sheet,高效便捷。
    linux 或者 mac 都可以安装
    a132811
        7
    a132811  
    OP
       2018-12-25 20:38:34 +08:00
    @jindaodama
    谢谢推荐。
    不过我是它希望作为一个 lib 使用。比如提供`xlparser.parse`, `xlparser.saveCsv`

    ```
    >>> from xlparser import parse
    >>> rows = parse('some.xlsx')
    >>> list(rows)
    [['foo', 'bar'], ['看', '我', '变']]
    ```
    另外处理某些 xls 出现的卡死问题(依赖 column_dimensions 本身是不可靠的)


    @pmyile 周末忘了说,已经支持多 sheet
    usboy
        8
    usboy  
       2019-01-18 19:10:33 +08:00   ❤️ 1
    小需求可以用在线工具: https://tableconvert.com 可以通过 import 导入 Excel,转出成 JSON,CSV,SQL,Markdown 各种格式
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3229 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.