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

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

  •  
  •   LeegoYih ·
    yihleego · 227 天前 · 2223 次点击
    这是一个创建于 227 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    9 条回复    2023-10-26 10:01:48 +08:00
    gujigujij
        1
    gujigujij  
       227 天前
    libreoffice 打开 View -> Page Break , 然后拖动蓝线设置分页位置
    LeegoYih
        2
    LeegoYih  
    OP
       227 天前
    @gujigujij 是在 Linux 容器环境中使用,只能用命令行,没法使用 GUI
    ohayoo
        3
    ohayoo  
       227 天前   ❤️ 1
    窝草,同一个世界,同一个需求,我老婆也是工作需要把一些服务器的巡检报告发给各个医院,什么从接口拉报告,筛选报告,修改文件名,邮件发送等等 python 都好弄,就这个 excel 导出为 pdf 始终没搞定,最后逼得没办法在 Windows 下用按键精灵了
    MarlonFan
        4
    MarlonFan  
       227 天前   ❤️ 1
    之前遇到过类似场景, 后来解决方案是 excel 渲染成 html, 然后 html 转 pdf
    gujigujij
        5
    gujigujij  
       227 天前   ❤️ 1
    之前我是这么做的, 在本地设置好分页把 excel 存成模板, 服务器上操作模板, (注意本地和线上 libreoffice 版本要一致)。 @LeegoYih
    Latin
        6
    Latin  
       226 天前
    pandas -> html -> wkhtmltopdf -> pdf
    jinja2 -> html -> wkhtmltopdf -> pdf
    自己先对源数据预处理 自己写 html 样式
    LeegoYih
        7
    LeegoYih  
    OP
       226 天前
    @Latin 有很多不同格式的 Excel ,每个都要写 HTML 有点太累了,后续还有各种非标的格式,看来没有简单的方案😂
    vialon17
        8
    vialon17  
       224 天前
    可以试试 openpyxl 和 win32 ,走系统的 pdf 打印途径,
    顺便可以设置正常 excel 打印的方式。
    LeegoYih
        9
    LeegoYih  
    OP
       184 天前
    目前使用一种很绕的方案:
    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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2885 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:30 · PVG 17:30 · LAX 02:30 · JFK 05:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.