Python 操作 Word 文档属性与字数统计方法详解

0 阅读4分钟

小李是刚入职场的行政助理,这天领导扔给他30份项目报告,要求统计每份报告的字数,还要提取创建时间和最后修改作者。手动打开一个个Word文档复制粘贴,显然不现实,小李决定用Python试试。

准备工作:安装与导入

操作Word文档最常用的库是python-docx。它只支持.docx格式,老旧的.doc格式需要先转换。

pip install python-docx

安装完成后,在代码中导入。处理日期时间还需要datetime库:

from docx import Document
from datetime import datetime

读取文档属性

Word文档的属性就像文件的“身份证”,记录了标题、作者、创建时间、修改次数等信息。这些信息藏在文档的“Core Properties”部分,python-docx可以轻松读取。

def get_doc_properties(doc_path):
    doc = Document(doc_path)
    core_props = doc.core_properties
    
    info = {
        "标题": core_props.title,
        "作者": core_props.author,
        "分类": core_props.category,
        "状态": core_props.content_status,
        "创建时间": core_props.created,
        "修改时间": core_props.modified,
        "最后保存者": core_props.last_modified_by,
        "修订次数": core_props.revision
    }
    return info

props = get_doc_properties("项目报告.docx")
for key, value in props.items():
    print(f"{key}{value}")

输出结果类似于:

标题: 2024年度项目总结
作者: 张三
创建时间: 2024-12-01 10:30:00
最后保存者: 李四
修订次数: 5

注意createdmodified返回的是datetime对象,可以直接用strftime格式化。

字数统计:主体内容

字数统计稍微复杂些。文档里的文字分散在不同的地方:段落、表格、页眉页脚、文本框。先统计最常见的主体段落和表格。

def count_word_document(doc_path):
    doc = Document(doc_path)
    
    total_chars = 0
    
    for paragraph in doc.paragraphs:
        text = paragraph.text.strip()
        total_chars += len(text)
    
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                text = cell.text.strip()
                total_chars += len(cell.text)
    
    return total_chars

word_count = count_word_document("项目报告.docx")
print(f"文档总字符数: {word_count}")

这里用len(text)统计字符数。对中文来说,每个汉字算1个字符。英文场景下可能需要按空格分词来统计单词数,用len(text.split())即可。

进阶字数统计:包含页眉页脚

页眉页脚往往藏着重要信息,比如公司名称、文档版本。统计它们能让字数更完整。

def count_with_headers(doc_path):
    doc = Document(doc_path)
    total_chars = 0
    
    for paragraph in doc.paragraphs:
        total_chars += len(paragraph.text)
    
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                total_chars += len(cell.text)
    
    for section in doc.sections:
        header = section.header
        for paragraph in header.paragraphs:
            total_chars += len(paragraph.text)
        
        footer = section.footer
        for paragraph in footer.paragraphs:
            total_chars += len(paragraph.text)
    
    return total_chars

通过doc.sections遍历每个节,再分别访问页眉和页脚的段落。

注意事项

段落与Run的区别python-docx把一段文字拆成多个Run对象,每个Run是一段样式相同的连续文本。直接取paragraph.text会合并所有Run的文本,对字数统计没影响。但如果要统计带格式的文字数量,可以遍历paragraph.runs分别处理。

性能问题:几十上百页的文档,遍历所有表格和段落没问题。上千页的超长文档,建议分页读取或换用更底层的lxml直接解析XML。

分页统计python-docx本身不提供精确的页数统计。如果依赖分页符来分页,可以用paragraph.contains_page_break判断:

def estimate_pages(doc_path):
    doc = Document(doc_path)
    page_breaks = sum(1 for p in doc.paragraphs if p.contains_page_break)
    return page_breaks + 1

这种方法只对手动插入分页符的文档有效,纯自动排版的文档会返回1。

完整示例:批量处理脚本

把上面内容整合成一个批量处理脚本:

import os
from docx import Document
from datetime import datetime

def analyze_doc(doc_path):
    try:
        doc = Document(doc_path)
        props = doc.core_properties
        
        char_count = 0
        for para in doc.paragraphs:
            char_count += len(para.text)
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    char_count += len(cell.text)
        
        return {
            "文件": os.path.basename(doc_path),
            "字符数": char_count,
            "作者": props.author,
            "创建时间": props.created.strftime("%Y-%m-%d"if props.created else "未知"
        }
    except Exception as e:
        print(f"处理失败 {doc_path}{e}")
        return None

folder = "./reports"
for filename in os.listdir(folder):
    if filename.endswith(".docx"):
        full_path = os.path.join(folder, filename)
        result = analyze_doc(full_path)
        if result:
            print(f"{result['文件']} - {result['字符数']}字 - {result['作者']}")

跑一遍脚本,30份报告的字数和作者信息就全部提取出来了。小李把结果导出到Excel,圆满完成了任务。

总结

python-docx处理Word文档属性与字数统计,核心就三点:doc.core_properties获取元数据、遍历doc.paragraphsdoc.tables统计文字、doc.sections访问页眉页脚。代码量不大,却能把重复劳动彻底自动化。下次领导再丢来一堆文档,就知道怎么优雅应对了。