Package not found at 'path/to/file.docx'
回答
收藏

Package not found at 'path/to/file.docx'

掘金喵
2024-06-26 21:35·浏览量:178
掘金喵
发布于 2024-06-26 21:35178浏览

需求把WORD中加粗的文字改变颜色,
使用了魔法指令,运行后出错,求解法~# 使用此指令前,先安装必要的python库,使用以下命令
# pip install python-docx

from docx import Document
from docx.shared import RGBColor

from xbot import print
from typing import *

def change_bold_text_color(file_path, color):
    """
    title: 修改加粗文字的颜色
    description: 将Word文档中所有加粗的文字修改为指定的颜色
    inputs: 
        - file_path (file): Word文档的文件路径,eg: "path/to/file.docx"
        - color (str): 颜色的RGB值,eg: "FF0000"表示红色
    outputs: 
        - None
    """
    doc = Document(file_path)
    
    for paragraph in doc.paragraphs:
        for run in paragraph.runs:
            if run.bold:
                run.font.color.rgb = RGBColor.from_string(color)
    
    doc.save(file_path)

收藏
全部回答1
最新
发布回答
回答