You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编辑Excel转PDF时遇OpenPyXL格式警告,求修复方案

解决openpyxl处理Excel时的条件格式与未知扩展警告问题

现有一段Python代码,用于编辑指定Excel文档并将其转换为PDF格式,功能可正常执行,但运行时会抛出两个UserWarning,需要修复以消除警告,确保功能完美运行。

原代码

from win32com import client
import openpyxl
import datetime
from openpyxl.formatting.rule import Rule
from openpyxl.formatting.rule import DataBar, FormatObject

Previous_Date = datetime.datetime.today() - datetime.timedelta(days=2)
Previous_Date_f = Previous_Date.strftime("%m/%d/%Y")


def date():
    wb = openpyxl.load_workbook("09.xlsx", read_only=False, data_only=False, keep_links=True)
    ws = wb.active
    ws["B1"] = Previous_Date_f
    ws["B2"] = Previous_Date_f
    first = FormatObject(type='min')
    second = FormatObject(type='max')
    data_bar = DataBar(cfvo=[first, second], color="32CD32", showValue=None, minLength=None, maxLength=None)
    rule = Rule(type='dataBar', dataBar=data_bar)
    ws.conditional_formatting.add("F5:F52", rule)
    wb.save(filename='n09.xlsx')


def pdf():
    excel = client.Dispatch("Excel.Application")
    sheets = excel.Workbooks.Open("../Desktop/new/n09.xlsx")
    work_sheets = sheets.Worksheets[0]
    work_sheets.PageSetup.Orientation = 2
    work_sheets.ExportAsFixedFormat(0, "../Desktop/new/n09.pdf")


if __name__ == "__main__":
    date()
    pdf()

出现的警告信息

UserWarning: Unknown extension is not supported and will be removed
  warn(msg)
UserWarning: Conditional Formatting extension is not supported and will be removed
  warn(msg)

警告原因

  1. 原Excel文件(09.xlsx)包含openpyxl不支持的自定义扩展或格式特性,openpyxl加载并保存时会移除这些特性,因此抛出警告。
  2. openpyxl对Excel条件格式的支持并不完全,添加数据条格式时触发了“条件格式扩展不支持”的兼容性警告。

修复方案

方案1:使用win32com完成所有Excel编辑操作(彻底解决警告)

直接调用本地Excel应用的API完成日期写入和条件格式设置,完全兼容Excel的所有特性,不会出现兼容性警告。修改后的完整代码如下:

from win32com import client
import datetime

Previous_Date = datetime.datetime.today() - datetime.timedelta(days=2)
Previous_Date_f = Previous_Date.strftime("%m/%d/%Y")


def date():
    # 用win32com打开原Excel文件
    excel = client.Dispatch("Excel.Application")
    excel.Visible = False  # 后台运行,不显示Excel窗口
    wb = excel.Workbooks.Open("09.xlsx")
    ws = wb.ActiveSheet
    
    # 写入日期
    ws.Range("B1").Value = Previous_Date_f
    ws.Range("B2").Value = Previous_Date_f
    
    # 添加数据条条件格式
    cf = ws.Range("F5:F52").FormatConditions.AddDatabar()
    cf.BarColor.Color = 0x32CD32  # 对应绿色的RGB值
    cf.ShowValue = False
    
    # 保存修改后的文件
    wb.SaveAs("../Desktop/new/n09.xlsx")
    wb.Close()
    excel.Quit()


def pdf():
    excel = client.Dispatch("Excel.Application")
    excel.Visible = False
    sheets = excel.Workbooks.Open("../Desktop/new/n09.xlsx")
    work_sheets = sheets.Worksheets[0]
    work_sheets.PageSetup.Orientation = 2  # 横向排版
    work_sheets.ExportAsFixedFormat(0, "../Desktop/new/n09.pdf")
    sheets.Close()
    excel.Quit()


if __name__ == "__main__":
    date()
    pdf()

方案2:保留openpyxl并抑制警告(快速临时解决)

如果不想替换openpyxl的逻辑,可以在代码开头添加警告抑制代码,隐藏这些UserWarning,但需要注意:此方法仅隐藏警告,原Excel中的不支持扩展仍会被移除,若这些扩展对业务有影响,不建议使用。

修改后的代码开头部分:

import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")

from win32com import client
import openpyxl
import datetime
from openpyxl.formatting.rule import Rule
from openpyxl.formatting.rule import DataBar, FormatObject

# 后续代码保持不变...

内容的提问来源于stack exchange,提问作者arrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:15:38