推荐学习书目
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
LeegoYih
V2EX  ›  Python

Python 通过 Excel 导出 PDF 有什么好的工具?

  •  
  •   LeegoYih ·
    yihleego · Sep 12, 2023 · 3354 views
    This topic created in 1000 days ago, the information mentioned may be changed or developed.

    主要运行在 Linux 环境,目前使用 libreoffice ,但是有些 Excel 表格列比较多,导出的 PDF 分页了,有没有办法控制导出的样式,比如缩放、横向导出?看了一下 libreoffice 文档好像没有。

    其他的工具像 xlrd 、reportlab 都不太好用。

    9 replies    2023-10-26 10:01:48 +08:00
    gujigujij
        1
    gujigujij  
       Sep 12, 2023
    libreoffice 打开 View -> Page Break , 然后拖动蓝线设置分页位置
    LeegoYih
        2
    LeegoYih  
    OP
       Sep 13, 2023
    @gujigujij 是在 Linux 容器环境中使用,只能用命令行,没法使用 GUI
    ohayoo
        3
    ohayoo  
       Sep 13, 2023   ❤️ 1
    窝草,同一个世界,同一个需求,我老婆也是工作需要把一些服务器的巡检报告发给各个医院,什么从接口拉报告,筛选报告,修改文件名,邮件发送等等 python 都好弄,就这个 excel 导出为 pdf 始终没搞定,最后逼得没办法在 Windows 下用按键精灵了
    MarlonFan
        4
    MarlonFan  
       Sep 13, 2023   ❤️ 1
    之前遇到过类似场景, 后来解决方案是 excel 渲染成 html, 然后 html 转 pdf
    gujigujij
        5
    gujigujij  
       Sep 13, 2023   ❤️ 1
    之前我是这么做的, 在本地设置好分页把 excel 存成模板, 服务器上操作模板, (注意本地和线上 libreoffice 版本要一致)。 @LeegoYih
    Latin
        6
    Latin  
       Sep 14, 2023
    pandas -> html -> wkhtmltopdf -> pdf
    jinja2 -> html -> wkhtmltopdf -> pdf
    自己先对源数据预处理 自己写 html 样式
    LeegoYih
        7
    LeegoYih  
    OP
       Sep 14, 2023
    @Latin 有很多不同格式的 Excel ,每个都要写 HTML 有点太累了,后续还有各种非标的格式,看来没有简单的方案😂
    vialon17
        8
    vialon17  
       Sep 16, 2023
    可以试试 openpyxl 和 win32 ,走系统的 pdf 打印途径,
    顺便可以设置正常 excel 打印的方式。
    LeegoYih
        9
    LeegoYih  
    OP
       Oct 26, 2023
    目前使用一种很绕的方案:
    1. 使用 Luckyexcel 和 Luckysheet 写了个 HTML ,主要用于导入 Excel ,通过 JS 将渲染到 Canvas 上的表格导出为图片。


    2. 使用 Selenium 控制无头浏览器本地打开 HTML ,会自动吐出图片文件。
    ```
    # 打开本地模板页面
    driver.get("file://" + os.path.abspath("templates/excel_printer/index.html"))
    driver.set_download_directory(os.path.abspath(image_dir))
    # 上传 Excel 文件
    upload_input = driver.find_element(by=By.ID, value="inputFile")
    upload_input.send_keys(os.path.abspath(excel_path))
    ```

    3. 最后用 Python 将图片转化为 PDF 文件。

    整个处理过程非常扭曲,且 Luckysheet 有很多 BUG:公式不识别、边框不显示等等。
    作者去做付费版本,开源版本没人管了,还是慎用吧。


    https://github.com/dream-num/Luckyexcel
    https://github.com/dream-num/Luckysheet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   965 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:59 · PVG 05:59 · LAX 14:59 · JFK 17:59
    ♥ Do have faith in what you're doing.