【办公类-89-02】20250424会议记录模版WORD自动添加空格补全下划线

发布于:2025-05-01 ⋅ 阅读:(20) ⋅ 点赞:(0)

背景需求

4月23日听了一个MJB的征文培训,需要写会议记录

把资料黏贴到模版后,发现每行需要有画满下划线

 原来做这套资料,就是手动按空格到一行末,有空格才会出现下划线,也就是要按很多的空格(凑满一行)

或者一行行的复制复制,因为文字有长短,所以后面的空格也有长有短

需求:

Python批量自动给最后添加适合的空格数,自动实现没有文字,但有空格,有下划线

第1问

代码展示

from docx import Document


def add_solid_underline_with_spaces(doc_path, output_path, target_length=29):
    """
    为Word文档每段添加实线下划线,并用全角空格填充至指定长度
    (仅对每段最后一行末尾添加空格和下划线)
    
    参数:
        doc_path (str): 输入文档路径
        output_path (str): 输出文档路径
        target_length (int): 目标字符宽度(中文算2,英文算1)
    """
    # 加载文档
    doc = Document(doc_path)
    
    for paragraph in doc.paragraphs:
        if paragraph.text.strip():  # 只处理非空段落
            # 获取段落的最后一行
            lines = paragraph.text.split('\n')
            last_line = lines[-1] if lines else ""
            
            # 计算最后一行的字符宽度
            current_width = sum(2 if ord(c) > 127 else 1 for c in last_line)
            
            # 计算需要填充的全角空格数
            fill_count = max(0, target_length - current_width)

            # 计算需要填充的全角空格数
            fill_count = max(0, target_length - current_width)
            
            # print(f"段落内容: '{text}'")
            print(f"当前宽度: {current_width}, 需要填充空格数: {fill_count}\n")
            
            # 为原有内容添加实线下划线
            for run in paragraph.runs:
                run.font.underline = True  # 设置为单实线
            
            # 仅在最后添加全角空格并带下划线
            if fill_count > 0:
                # 清除原有换行符(如果有)
                if paragraph.runs and paragraph.runs[-1].text.endswith('\n'):
                    paragraph.runs[-1].text = paragraph.runs[-1].text.rstrip('\n')
                
                fill_run = paragraph.add_run("\u3000"  * fill_count)  # 使用全角空格
                fill_run.font.underline = True
                
                # 恢复原有换行符(如果有)
                if len(lines) > 1:
                    paragraph.add_run().add_break()

    # 保存文档
    doc.save(output_path)

# 使用示例
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\工具运用'
input_path = path + r'\附件1工具运用训会议记录(2025.4.23).docx'
output_path = path + r'\附件1工具运用训会议记录(2025.4.23)_实线下划线.docx'
add_solid_underline_with_spaces(input_path, output_path, target_length=29)

出现很多的方块

把全角改成空格

结果方块没有了,但是下划线不够长,

 分析:预设29个空格位置,已有字符超过29个,就不添加空格了

所以把预设空格数改到最大1000

运行后,都有空格了

结果显示

但是里面有大量的空格,会不会占空间?

结果显示:

如果设置1000字以上,空格太多,大小1.13

如果设置500字以上,大小变为22.3K

问题:

可是我并不知道每一段的总字数是多少,现在给所有段落默认添加1000字符,如果一段文字大于1000字,还是会没有空格和下划线。同时其他段落标题只有4-10个字,根本不需要填充1000字空格。

第2问:计算原有字符数+额外加多少空格下划线

手动测算一行有几个空

默认加78空,保证下划线肯定能撑满

代码展示

from docx import Document

z=78

def add_dynamic_underline(doc_path, output_path, extra_length=z):
    """
    为Word文档每段添加实线下划线,动态计算每段长度并额外增加指定字符数
    用全角空格填充至(实际长度+extra_length)的宽度
    
    参数:
        doc_path (str): 输入文档路径
        output_path (str): 输出文档路径
        extra_length (int): 每段额外增加的字符宽度
    """
    # 加载文档
    doc = Document(doc_path)
    
    for paragraph in doc.paragraphs:
        if paragraph.text.strip():  # 只处理非空段落
            # 获取段落的最后一行
            lines = paragraph.text.split('\n')
            last_line = lines[-1] if lines else ""
            
            # 计算最后一行的字符宽度(中文算2,英文算1)
            current_width = sum(2 if ord(c) > 127 else 1 for c in last_line)
            
            # 动态设置目标长度 = 当前宽度 + 额外长度
            target_length = current_width + extra_length
            
            # 计算需要填充的全角空格数
            fill_count = max(0, target_length - current_width)
            
            print(f"段落内容: '{last_line}'")
            print(f"当前宽度: {current_width}, 目标宽度: {target_length}, 需要填充空格数: {fill_count}\n")
            
            # 为原有内容添加实线下划线
            for run in paragraph.runs:
                run.font.underline = True  # 设置为单实线
            
            # 仅在最后添加全角空格并带下划线
            if fill_count > 0:
                # 清除原有换行符(如果有)
                if paragraph.runs and paragraph.runs[-1].text.endswith('\n'):
                    paragraph.runs[-1].text = paragraph.runs[-1].text.rstrip('\n')
                
                fill_run = paragraph.add_run(" " * fill_count)  # 使用全角空格
                fill_run.font.underline = True
                
                # 恢复原有换行符(如果有)
                if len(lines) > 1:
                    paragraph.add_run().add_break()

    # 保存文档
    doc.save(output_path)

# 使用示例
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\工具运用'
input_path = path + r'\附件1工具运用训会议记录(2025.4.23).docx'
output_path = path + r'\附件1工具运用训会议记录(2025.4.23)_动态下划线.docx'
add_dynamic_underline(input_path, output_path, extra_length=z)

虽然打印时看不见,但我觉得右边距上一推灰点和回车,还是不好看,希望右边距上的点子不要出现。

第3问

计算一行可以有多少个字符

手动输入满一行

from docx import Document



def count_width_per_line(doc_path):
    """统计 Word 文档中每一行的宽度(中文=2,英文=1)"""
    doc = Document(doc_path)
    
    for i, paragraph in enumerate(doc.paragraphs, 1):
        if not paragraph.text.strip():  # 跳过空行
            continue
        
        lines = paragraph.text.split('\n')  # 按换行符分割
        for j, line in enumerate(lines, 1):
            if not line.strip():  # 跳过空行
                continue
            
            # 计算宽度(中文=2,英文=1)
            width = sum(2 if ord(c) > 127 else 1 for c in line)
            print(f"第 {i} 段,第 {j} 行 | 宽度: {width} | 字符数: {len(line)} | 内容: '{line}'")
    
    print("\n统计完成!")

# 使用示例
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\工具运用'
doc_path = path + r'\附件1工具运用训会议记录(2025.4.23).docx'
count_width_per_line(doc_path)

计算一行可以有多少个字符=69个

第4问:每段最后一行有有几个汉字,69减去最后一行的汉字的树数目就是添加额外的空格数量

把都是汉字字符,是双数字符,尝试把69改成70

正好,都显示满了

代码展示

'''
会议记录自动补全下划线
 根据字数添加补全剩余空格下划线
deepseek \阿夏
20250424
'''
from docx import Document

# 每行标准宽度(中文=2,英文=1)
h = 70

def add_dynamic_underline(doc_path, output_path):
    """
    为Word文档每段添加实线下划线,动态计算每段行数并调整下划线长度
    并保持原有字体格式
    
    参数:
        doc_path (str): 输入文档路径
        output_path (str): 输出文档路径
    """
    doc = Document(doc_path)
    
    for paragraph in doc.paragraphs:
        if paragraph.text.strip():  # 只处理非空段落
            # 获取段落的最后一行
            lines = paragraph.text.split('\n')
            last_line = lines[-1] if lines else ""
            
            # 计算最后一行的字符宽度(中文算2,英文算1)
            current_width = sum(2 if ord(c) > 127 else 1 for c in last_line)
            
            # 计算该段有多少行(向上取整)
            line_count = current_width // h
            if line_count <1:
                line_count = 1
                extra_length = h - current_width 
            else:
                extra_length = h - (current_width - line_count*h)
                
            print(f"段落内容: '{last_line}'")
            print(f"当前宽度: {current_width},汉字和英文的字符长度:'{len(last_line)}', 每行宽度: {h}, 行数: {line_count}")
            print(f"额外下划线长度: {extra_length}\n")
    
            # print(f"当前宽度: {current_width}, 目标宽度: {target_length}, 需要填充空格数: {fill_count}\n")
            
            
            # 为原有内容添加实线下划线
            for run in paragraph.runs:
                run.font.underline = True  # 设置为单实线
            
            # 仅在最后添加全角空格并带下划线
            if extra_length > 0:
                # 清除原有换行符(如果有)
                if paragraph.runs and paragraph.runs[-1].text.endswith('\n'):
                    paragraph.runs[-1].text = paragraph.runs[-1].text.rstrip('\n')
                
                # 获取最后一个 run 的字体格式
                last_run = paragraph.runs[-1] if paragraph.runs else None
                
                # 添加空格,并复制原有格式
                fill_run = paragraph.add_run(" " * extra_length)
                if last_run:  # 如果存在原有 run,则复制其字体格式
                    fill_run.font.name = last_run.font.name  # 字体
                    fill_run.font.size = last_run.font.size  # 字号
                    fill_run.font.bold = last_run.font.bold  # 加粗
                    fill_run.font.italic = last_run.font.italic  # 斜体
                fill_run.font.underline = True  # 设置下划线
                
                # 恢复原有换行符(如果有)
                if len(lines) > 1:
                    paragraph.add_run().add_break()

    # 保存文档
    doc.save(output_path)

# 使用示例
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\工具运用'
input_path = path + r'\附件1工具运用训会议记录(2025.4.23).docx'
output_path = path + r'\附件1工具运用训会议记录(2025.4.23)_动态下划线.docx'
add_dynamic_underline(input_path, output_path)

测试效果

删除空行,然后把内容变成10-1000字的段落

ok,完美实现补全下划线的目标。

后续做成exe,GUI界面,选择文件夹,发给不同的课题组成员补会议记录用(贴完文字后,用Python自动添加一下每行最后空白的下划线)让做的资料的文本版式更好看,


网站公告

今日签到

点亮在社区的每一天
去签到