Python 编程:Word 操作

将Word转换为PPT的几种方法及编程实现 本文将介绍几种方法来实现将Word文档转换为PPT演示文稿,并提供相应的编程示例代码。综上所述,我们介绍了使用Python和python-pptx库、VBA宏以及在线转换工具这三种方法来将Word文档转换为PPT演示文稿。你可以搜索并使用这些工具,它们通常提供简单易用的界面来上传Word文档并下载转换后的PPT演示文稿。然后,我们遍历Word文档的每个段落,创建一个新的幻灯片,并将段落内容设置为幻灯片的标题。库创建一个新的PPT演示文稿,并将每个段落的内容作为一个幻灯片的标题。方法三:使用在线转换工具。 阅读详情

Python 生态中有多个用于操作 Word 文档的库,每个库都有其特点和适用场景。

1. docx2python

特点:

  • 专注于从 Word 文档中提取内容

  • 保留文档结构信息

  • 支持表格、文本、图片等元素的提取

安装:

pip install docx2python

2. pywin32 (仅Windows)

特点:

  • 通过 COM 接口直接操作 Microsoft Word

  • 功能最全面,可以调用 Word 的所有功能

  • 仅适用于 Windows 系统

  • 需要安装 Microsoft Word

安装:

pip install pywin32

3. docx-mailmerge

特点:

  • 专注于邮件合并功能

  • 简化了模板填充操作

  • 基于 python-docx 构建

安装:

pip install docx-mailmerge

4. python-docx-template

特点:

  • 支持类似Jinja2的模板语法

  • 可以处理条件语句、循环等复杂逻辑

  • 支持图片、表格等复杂元素的替换

安装:

pip install docxtpl

5. mammoth (转换工具)

特点:

  • 专注于 Word 到 HTML 的转换

  • 保留基本格式和结构

  • 支持自定义样式映射

安装:

pip install mammoth

6. aspose-words

特点:

  • 商业库,功能强大

  • 支持多种文档格式互转

  • 跨平台支持

安装:

pip install aspose-words

库对比总结

库名称主要功能平台要求许可证适合场景
python-docx创建/修改docx跨平台MIT基本Word操作
docx2python内容提取跨平台MIT数据提取
pywin32全功能Word操作WindowsMIT高级自动化
docx-mailmerge邮件合并跨平台MIT模板填充
docxtpl模板渲染跨平台MIT复杂模板
mammothWord转HTML跨平台BSD内容发布
aspose-words全功能文档处理跨平台商业企业级应用

选择建议

  1. 基本文档生成:python-docx 是最佳选择

  2. 内容提取:使用 docx2python

  3. Windows环境高级操作:pywin32 提供最全面的功能

  4. 模板填充:docx-mailmerge 或 python-docx-template

  5. 文档转换:mammoth 或 aspose-words

  6. 企业级应用:考虑商业库 aspose-words

高级技巧:结合使用多个库

from docx import Document
from docx2python import docx2python
from docxtpl import DocxTemplate

# 1. 使用docx2python提取模板结构
doc_content = docx2python('template.docx')
print("模板包含以下段落:", doc_content.text)

# 2. 使用python-docx创建新文档
doc = Document()
doc.add_heading('合并后的文档', level=1)

# 3. 使用docxtpl渲染复杂部分
tpl = DocxTemplate('complex_part.docx')
context = {'data': [...]}
tpl.render(context)
tpl.save('temp.docx')

# 4. 将渲染结果合并到主文档
temp_doc = Document('temp.docx')
for para in temp_doc.paragraphs:
    doc.add_paragraph(para.text)

# 保存最终文档
doc.save('final_output.docx')

Python-docx 库

python-docx 是一个用于创建和修改 Microsoft Word (.docx) 文件的 Python 库。下面是该库的全面介绍和使用指南。

1. 安装与基本使用

安装

pip install python-docx

创建新文档

from docx import Document

# 创建新文档
doc = Document()
doc.add_paragraph('Hello, Word!')
doc.save('hello.docx')

2. 文档结构操作

2.1 段落操作

# 添加段落
paragraph = doc.add_paragraph('这是一个段落')

# 添加带样式的段落
paragraph = doc.add_paragraph('这是一个标题', style='Heading 1')

# 分段添加文本
paragraph.add_run('加粗文本').bold = True
paragraph.add_run('普通文本')
paragraph.add_run('斜体文本').italic = True

# 获取所有段落
for para in doc.paragraphs:
    print(para.text)

2.2 标题设置

# 添加各级标题
doc.add_heading('一级标题', level=1)
doc.add_heading('二级标题', level=2)
doc.add_heading('三级标题', level=3)

3. 文本格式设置

3.1 字体格式

from docx.shared import Pt, RGBColor
from docx.oxml.ns import qn

# 设置中文字体
paragraph = doc.add_paragraph()
run = paragraph.add_run('中文字体测试')
run.font.name = '微软雅黑'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑')

# 设置字体大小
run.font.size = Pt(14)

# 设置字体颜色
run.font.color.rgb = RGBColor(255, 0, 0)  # 红色

# 其他格式
run.bold = True       # 加粗
run.italic = True     # 斜体
run.underline = True  # 下划线

3.2 段落格式

from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Inches

paragraph = doc.add_paragraph('对齐和缩进示例')

# 对齐方式
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 居中
# 其他选项: LEFT, RIGHT, JUSTIFY, DISTRIBUTE

# 缩进
paragraph.paragraph_format.left_indent = Inches(0.5)  # 左缩进0.5英寸
paragraph.paragraph_format.right_indent = Inches(0.5) # 右缩进
paragraph.paragraph_format.first_line_indent = Inches(0.5)  # 首行缩进

# 行距
paragraph.paragraph_format.line_spacing = 1.5  # 1.5倍行距
paragraph.paragraph_format.space_after = Pt(12)  # 段后间距12磅
paragraph.paragraph_format.space_before = Pt(12) # 段前间距

4. 表格操作

4.1 创建表格

# 添加3行4列的表格
table = doc.add_table(rows=3, cols=4)

# 填充表格数据
for i in range(3):
    for j in range(4):
        table.cell(i, j).text = f'行{i+1}列{j+1}'

# 另一种添加表格的方式(推荐)
data = [
    ['姓名', '年龄', '性别'],
    ['张三', '25', '男'],
    ['李四', '30', '女']
]

table = doc.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
for i in range(3):
    hdr_cells[i].text = data[0][i]

for row_data in data[1:]:
    row_cells = table.add_row().cells
    for i in range(3):
        row_cells[i].text = row_data[i]

4.2 表格样式

from docx.enum.table import WD_TABLE_ALIGNMENT, WD_CELL_VERTICAL_ALIGNMENT
from docx.enum.table import WD_ROW_HEIGHT_RULE

# 表格对齐
table.alignment = WD_TABLE_ALIGNMENT.CENTER

# 设置行高
table.rows[0].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
table.rows[0].height = Pt(30)

# 单元格垂直对齐
for row in table.rows:
    for cell in row.cells:
        cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

# 应用表格样式
table.style = 'Light Shading Accent 1'
# 其他内置样式: 'Table Grid', 'Light List', 'Medium Shading 1' 等

5. 图片与形状

5.1 插入图片

from docx.shared import Cm

doc.add_picture('image.png', width=Cm(10))  # 宽度10厘米
# 也可以使用 Inches(4) 表示4英寸

# 设置图片居中对齐
last_paragraph = doc.paragraphs[-1]
last_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

5.2 插入形状

from docx.enum.shape import MSO_SHAPE

# 添加一个矩形
shape = doc.add_shape(MSO_SHAPE.RECTANGLE, left=Cm(5), top=Cm(5), 
                     width=Cm(5), height=Cm(3))
shape.text = '这是一个矩形'

# 添加线条
doc.add_shape(MSO_SHAPE.LINE_INVERSE, 
              left=Cm(2), top=Cm(2), 
              width=Cm(10), height=Cm(0))

6. 页眉页脚与分节

6.1 页眉页脚

# 添加页眉
header = doc.sections[0].header
header_para = header.paragraphs[0]
header_para.text = "这是页眉 - 左对齐"
header_para.add_run("右对齐部分").alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT

# 添加页脚
footer = doc.sections[0].footer
footer_para = footer.paragraphs[0]
footer_para.text = "页码: "
footer_para.add_run("1").bold = True

6.2 分节与页面设置

from docx.enum.section import WD_SECTION

# 添加新节
doc.add_section(WD_SECTION.NEW_PAGE)  # 新页分节
# 其他选项: NEW_PAGE, CONTINUOUS, EVEN_PAGE, ODD_PAGE

# 页面设置
section = doc.sections[0]
section.page_height = Cm(29.7)  # A4高度
section.page_width = Cm(21)     # A4宽度
section.left_margin = Cm(2.54)  # 左边距
section.right_margin = Cm(2.54) # 右边距
section.top_margin = Cm(2.54)   # 上边距
section.bottom_margin = Cm(2.54) # 下边距
section.header_distance = Cm(1.25)  # 页眉距离
section.footer_distance = Cm(1.25)  # 页脚距离

7. 高级功能

7.1 样式管理

from docx.enum.style import WD_STYLE_TYPE

# 创建新样式
styles = doc.styles
new_style = styles.add_style('MyStyle', WD_STYLE_TYPE.PARAGRAPH)
new_style.font.name = '微软雅黑'
new_style.font.size = Pt(12)
new_style.font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
new_style.paragraph_format.line_spacing = 1.5

# 应用样式
doc.add_paragraph('自定义样式文本', style='MyStyle')

7.2 目录生成

# 添加目录(需要Word在打开文档后更新字段)
doc.add_paragraph().add_run().add_field('TOC \\o "1-3" \\h \\z \\u')

# 添加分页符
doc.add_page_break()

7.3 超链接

from docx.oxml.shared import OxmlElement
from docx.oxml.ns import qn

def add_hyperlink(paragraph, text, url):
    """添加超链接"""
    part = paragraph.part
    r_id = part.relate_to(
        url, 
        docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, 
        is_external=True
    )

    hyperlink = OxmlElement('w:hyperlink')
    hyperlink.set(qn('r:id'), r_id)

    new_run = OxmlElement('w:r')
    rPr = OxmlElement('w:rPr')
    new_run.append(rPr)
    new_run.text = text
    hyperlink.append(new_run)

    paragraph._p.append(hyperlink)
    return hyperlink

# 使用示例
para = doc.add_paragraph()
add_hyperlink(para, "百度", "https://www.baidu.com")

8. 读取现有文档

doc = Document('existing.docx')

# 读取段落
for para in doc.paragraphs:
    print(para.text)

# 读取表格
for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            print(cell.text)

# 读取页眉页脚
for section in doc.sections:
    print("页眉:", section.header.paragraphs[0].text)
    print("页脚:", section.footer.paragraphs[0].text)

9. 实际应用示例

9.1 生成报告

def generate_report(data):
    doc = Document()
    
    # 标题
    doc.add_heading('销售报告', level=1)
    
    # 基本信息
    doc.add_paragraph(f'生成日期: {datetime.now().strftime("%Y-%m-%d")}')
    doc.add_paragraph(f'报告周期: {data["start_date"]} 至 {data["end_date"]}')
    
    # 摘要
    doc.add_heading('执行摘要', level=2)
    doc.add_paragraph(data['summary'])
    
    # 销售数据表格
    doc.add_heading('销售数据', level=2)
    table = doc.add_table(rows=1, cols=4)
    table.style = 'Light Shading Accent 1'
    
    # 表头
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = '产品'
    hdr_cells[1].text = '数量'
    hdr_cells[2].text = '单价'
    hdr_cells[3].text = '总额'
    
    # 表格数据
    for item in data['sales']:
        row_cells = table.add_row().cells
        row_cells[0].text = item['product']
        row_cells[1].text = str(item['quantity'])
        row_cells[2].text = f"¥{item['price']:.2f}"
        row_cells[3].text = f"¥{item['quantity'] * item['price']:.2f}"
    
    # 图表
    doc.add_heading('销售趋势图', level=2)
    doc.add_paragraph('下图显示了销售趋势:')
    doc.add_picture('sales_chart.png', width=Cm(12))
    
    # 结论
    doc.add_heading('结论与建议', level=2)
    doc.add_paragraph(data['conclusion'])
    
    # 保存
    doc.save('sales_report.docx')

9.2 批量生成合同

def generate_contracts(template_path, clients):
    for client in clients:
        doc = Document(template_path)
        
        # 替换占位符
        replacements = {
            '{{client_name}}': client['name'],
            '{{date}}': datetime.now().strftime('%Y年%m月%d日'),
            '{{amount}}': f"¥{client['amount']:,.2f}",
            '{{service}}': client['service']
        }
        
        for para in doc.paragraphs:
            for key, value in replacements.items():
                if key in para.text:
                    para.text = para.text.replace(key, value)
        
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    for key, value in replacements.items():
                        if key in cell.text:
                            cell.text = cell.text.replace(key, value)
        
        # 保存
        doc.save(f"contract_{client['id']}.docx")

注意事项

  1. 兼容性:python-docx 只支持 .docx 格式(Word 2007及以后版本),不支持旧的 .doc 格式

  2. 样式限制:某些复杂的Word功能可能无法完全支持

  3. 文档打开:生成的文档需要在Word中打开后才能正确显示某些元素(如目录、页码等)

  4. 性能问题:处理大型文档时可能会有性能问题,建议分批处理

  5. 字体问题:中文字体需要特殊处理,确保同时设置西文和中文字体

通过 python-docx,您可以实现绝大多数常见的Word文档操作需求,从简单的文档生成到复杂的报告自动化都能胜任。

Word文档编辑器实现

下面介绍如何使用Python创建Word文档编辑器,主要使用python-docx库来处理Word文档,并结合PyQt5构建图形用户界面。

1. 环境准备

首先安装必要的库:

pip install python-docx PyQt5

2. 基本Word编辑器实现

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTextEdit, 
                             QFileDialog, QVBoxLayout, QWidget, 
                             QPushButton, QMessageBox, QFontComboBox,
                             QSpinBox, QHBoxLayout, QAction, QToolBar)
from PyQt5.QtGui import QTextCursor, QIcon
from PyQt5.QtCore import Qt
from docx import Document
from docx.shared import Pt
import os

class WordEditor(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Word编辑器")
        self.setGeometry(100, 100, 800, 600)
        
        self.current_file = None
        self.initUI()
        
    def initUI(self):
        # 主窗口部件
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        
        # 主布局
        self.main_layout = QVBoxLayout()
        
        # 创建工具栏
        self.create_toolbar()
        
        # 格式工具栏
        self.format_toolbar = QToolBar("格式工具栏")
        self.addToolBar(Qt.TopToolBarArea, self.format_toolbar)
        
        # 字体选择
        self.font_combo = QFontComboBox()
        self.font_combo.currentFontChanged.connect(self.change_font)
        self.format_toolbar.addWidget(self.font_combo)
        
        # 字号选择
        self.font_size = QSpinBox()
        self.font_size.setRange(8, 72)
        self.font_size.setValue(12)
        self.font_size.valueChanged.connect(self.change_font_size)
        self.format_toolbar.addWidget(self.font_size)
        
        # 加粗按钮
        self.bold_action = QAction(QIcon.fromTheme('format-text-bold'), "加粗", self)
        self.bold_action.setCheckable(True)
        self.bold_action.triggered.connect(self.toggle_bold)
        self.format_toolbar.addAction(self.bold_action)
        
        # 斜体按钮
        self.italic_action = QAction(QIcon.fromTheme('format-text-italic'), "斜体", self)
        self.italic_action.setCheckable(True)
        self.italic_action.triggered.connect(self.toggle_italic)
        self.format_toolbar.addAction(self.italic_action)
        
        # 下划线按钮
        self.underline_action = QAction(QIcon.fromTheme('format-text-underline'), "下划线", self)
        self.underline_action.setCheckable(True)
        self.underline_action.triggered.connect(self.toggle_underline)
        self.format_toolbar.addAction(self.underline_action)
        
        # 文本编辑区
        self.text_edit = QTextEdit()
        self.text_edit.cursorPositionChanged.connect(self.update_format_buttons)
        
        # 添加到布局
        self.main_layout.addWidget(self.text_edit)
        self.central_widget.setLayout(self.main_layout)
        
        # 状态栏
        self.statusBar().showMessage("就绪")
        
        # 创建菜单
        self.create_menus()
    
    def create_toolbar(self):
        toolbar = QToolBar("主工具栏")
        self.addToolBar(Qt.TopToolBarArea, toolbar)
        
        # 新建
        new_action = QAction(QIcon.fromTheme('document-new'), "新建", self)
        new_action.setShortcut("Ctrl+N")
        new_action.triggered.connect(self.new_file)
        toolbar.addAction(new_action)
        
        # 打开
        open_action = QAction(QIcon.fromTheme('document-open'), "打开", self)
        open_action.setShortcut("Ctrl+O")
        open_action.triggered.connect(self.open_file)
        toolbar.addAction(open_action)
        
        # 保存
        self.save_action = QAction(QIcon.fromTheme('document-save'), "保存", self)
        self.save_action.setShortcut("Ctrl+S")
        self.save_action.triggered.connect(self.save_file)
        self.save_action.setEnabled(False)
        toolbar.addAction(self.save_action)
        
        # 另存为
        save_as_action = QAction(QIcon.fromTheme('document-save-as'), "另存为", self)
        save_as_action.triggered.connect(self.save_file_as)
        toolbar.addAction(save_as_action)
        
        toolbar.addSeparator()
        
        # 打印
        print_action = QAction(QIcon.fromTheme('document-print'), "打印", self)
        print_action.setShortcut("Ctrl+P")
        print_action.triggered.connect(self.print_document)
        toolbar.addAction(print_action)
    
    def create_menus(self):
        menubar = self.menuBar()
        
        # 文件菜单
        file_menu = menubar.addMenu("文件")
        
        new_action = QAction("新建", self)
        new_action.setShortcut("Ctrl+N")
        new_action.triggered.connect(self.new_file)
        file_menu.addAction(new_action)
        
        open_action = QAction("打开...", self)
        open_action.setShortcut("Ctrl+O")
        open_action.triggered.connect(self.open_file)
        file_menu.addAction(open_action)
        
        file_menu.addSeparator()
        
        save_action = QAction("保存", self)
        save_action.setShortcut("Ctrl+S")
        save_action.triggered.connect(self.save_file)
        file_menu.addAction(save_action)
        
        save_as_action = QAction("另存为...", self)
        save_as_action.triggered.connect(self.save_file_as)
        file_menu.addAction(save_as_action)
        
        file_menu.addSeparator()
        
        print_action = QAction("打印...", self)
        print_action.setShortcut("Ctrl+P")
        print_action.triggered.connect(self.print_document)
        file_menu.addAction(print_action)
        
        file_menu.addSeparator()
        
        exit_action = QAction("退出", self)
        exit_action.setShortcut("Ctrl+Q")
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)
        
        # 编辑菜单
        edit_menu = menubar.addMenu("编辑")
        
        undo_action = QAction("撤销", self)
        undo_action.setShortcut("Ctrl+Z")
        undo_action.triggered.connect(self.text_edit.undo)
        edit_menu.addAction(undo_action)
        
        redo_action = QAction("重做", self)
        redo_action.setShortcut("Ctrl+Y")
        redo_action.triggered.connect(self.text_edit.redo)
        edit_menu.addAction(redo_action)
        
        edit_menu.addSeparator()
        
        cut_action = QAction("剪切", self)
        cut_action.setShortcut("Ctrl+X")
        cut_action.triggered.connect(self.text_edit.cut)
        edit_menu.addAction(cut_action)
        
        copy_action = QAction("复制", self)
        copy_action.setShortcut("Ctrl+C")
        copy_action.triggered.connect(self.text_edit.copy)
        edit_menu.addAction(copy_action)
        
        paste_action = QAction("粘贴", self)
        paste_action.setShortcut("Ctrl+V")
        paste_action.triggered.connect(self.text_edit.paste)
        edit_menu.addAction(paste_action)
        
        # 格式菜单
        format_menu = menubar.addMenu("格式")
        
        bold_action = QAction("加粗", self)
        bold_action.setShortcut("Ctrl+B")
        bold_action.setCheckable(True)
        bold_action.triggered.connect(self.toggle_bold)
        format_menu.addAction(bold_action)
        
        italic_action = QAction("斜体", self)
        italic_action.setShortcut("Ctrl+I")
        italic_action.setCheckable(True)
        italic_action.triggered.connect(self.toggle_italic)
        format_menu.addAction(italic_action)
        
        underline_action = QAction("下划线", self)
        underline_action.setShortcut("Ctrl+U")
        underline_action.setCheckable(True)
        underline_action.triggered.connect(self.toggle_underline)
        format_menu.addAction(underline_action)
    
    def new_file(self):
        self.text_edit.clear()
        self.current_file = None
        self.save_action.setEnabled(False)
        self.statusBar().showMessage("新建文档", 2000)
    
    def open_file(self):
        file_path, _ = QFileDialog.getOpenFileName(
            self, "打开Word文档", "", 
            "Word文档 (*.docx);;所有文件 (*)"
        )
        
        if file_path:
            try:
                doc = Document(file_path)
                self.text_edit.clear()
                
                full_text = []
                for para in doc.paragraphs:
                    full_text.append(para.text)
                
                self.text_edit.setPlainText("\n".join(full_text))
                self.current_file = file_path
                self.save_action.setEnabled(True)
                
                self.statusBar().showMessage(f"已打开: {file_path}", 3000)
                
            except Exception as e:
                QMessageBox.critical(self, "错误", f"无法打开文件:\n{str(e)}")
                self.statusBar().showMessage(f"打开文件失败: {str(e)}", 5000)
    
    def save_file(self):
        if not self.current_file:
            self.save_file_as()
            return
            
        try:
            doc = Document()
            text = self.text_edit.toPlainText()
            
            # 保存为段落
            for line in text.split('\n'):
                doc.add_paragraph(line)
            
            doc.save(self.current_file)
            self.statusBar().showMessage(f"文件已保存: {self.current_file}", 3000)
            
        except Exception as e:
            QMessageBox.critical(self, "错误", f"保存失败:\n{str(e)}")
            self.statusBar().showMessage(f"保存失败: {str(e)}", 5000)
    
    def save_file_as(self):
        file_path, _ = QFileDialog.getSaveFileName(
            self, "另存为Word文档", "", 
            "Word文档 (*.docx);;所有文件 (*)"
        )
        
        if file_path:
            # 确保扩展名是.docx
            if not file_path.endswith('.docx'):
                file_path += '.docx'
                
            self.current_file = file_path
            self.save_file()
            self.save_action.setEnabled(True)
    
    def print_document(self):
        # 简单打印功能
        printer = QPrinter()
        print_dialog = QPrintDialog(printer, self)
        
        if print_dialog.exec_() == QPrintDialog.Accepted:
            self.text_edit.print_(printer)
    
    def change_font(self, font):
        cursor = self.text_edit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        
        format = cursor.charFormat()
        format.setFontFamily(font.family())
        cursor.mergeCharFormat(format)
    
    def change_font_size(self, size):
        cursor = self.text_edit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        
        format = cursor.charFormat()
        format.setFontPointSize(size)
        cursor.mergeCharFormat(format)
    
    def toggle_bold(self):
        cursor = self.text_edit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        
        format = cursor.charFormat()
        format.setFontWeight(QFont.Bold if self.bold_action.isChecked() else QFont.Normal)
        cursor.mergeCharFormat(format)
    
    def toggle_italic(self):
        cursor = self.text_edit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        
        format = cursor.charFormat()
        format.setFontItalic(self.italic_action.isChecked())
        cursor.mergeCharFormat(format)
    
    def toggle_underline(self):
        cursor = self.text_edit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QTextCursor.WordUnderCursor)
        
        format = cursor.charFormat()
        format.setFontUnderline(self.underline_action.isChecked())
        cursor.mergeCharFormat(format)
    
    def update_format_buttons(self):
        cursor = self.text_edit.textCursor()
        format = cursor.charFormat()
        
        # 更新字体组合框
        self.font_combo.setCurrentFont(QFont(format.fontFamily()))
        
        # 更新字号
        self.font_size.setValue(format.fontPointSize())
        
        # 更新加粗、斜体、下划线按钮状态
        self.bold_action.setChecked(format.fontWeight() == QFont.Bold)
        self.italic_action.setChecked(format.fontItalic())
        self.underline_action.setChecked(format.fontUnderline())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    editor = WordEditor()
    editor.show()
    sys.exit(app.exec_())

3. 功能扩展实现

3.1 添加图片支持

def insert_image(self):
    file_path, _ = QFileDialog.getOpenFileName(
        self, "插入图片", "", 
        "图片文件 (*.png *.jpg *.jpeg *.bmp *.gif)"
    )
    
    if file_path:
        cursor = self.text_edit.textCursor()
        cursor.insertHtml(f'<img src="{file_path}" />')

3.2 添加表格支持

def insert_table(self):
    rows, ok1 = QInputDialog.getInt(self, "插入表格", "行数:", 3, 1, 20)
    cols, ok2 = QInputDialog.getInt(self, "插入表格", "列数:", 3, 1, 20)
    
    if ok1 and ok2:
        cursor = self.text_edit.textCursor()
        table_html = "<table border='1'>"
        for _ in range(rows):
            table_html += "<tr>"
            for _ in range(cols):
                table_html += "<td> </td>"
            table_html += "</tr>"
        table_html += "</table>"
        cursor.insertHtml(table_html)

3.3 添加页眉页脚支持

def add_header_footer(self):
    doc = Document()
    
    # 添加页眉
    header = doc.sections[0].header
    header_para = header.paragraphs[0]
    header_para.text = "这是页眉"
    
    # 添加正文
    doc.add_paragraph(self.text_edit.toPlainText())
    
    # 添加页脚
    footer = doc.sections[0].footer
    footer_para = footer.paragraphs[0]
    footer_para.text = "这是页脚"
    
    # 保存临时文件并打开
    temp_file = "temp_with_header_footer.docx"
    doc.save(temp_file)
    os.startfile(temp_file)  # Windows
    # 在macOS上使用: os.system(f"open {temp_file}")
    # 在Linux上使用: os.system(f"xdg-open {temp_file}")

4. 高级功能实现

4.1 文档格式转换

def convert_to_pdf(self):
    if not self.current_file:
        QMessageBox.warning(self, "警告", "请先保存Word文档")
        return
    
    try:
        from docx2pdf import convert
        pdf_path = os.path.splitext(self.current_file)[0] + ".pdf"
        convert(self.current_file, pdf_path)
        QMessageBox.information(self, "成功", f"已转换为PDF: {pdf_path}")
    except ImportError:
        QMessageBox.critical(self, "错误", "请先安装docx2pdf库: pip install docx2pdf")
    except Exception as e:
        QMessageBox.critical(self, "错误", f"转换失败:\n{str(e)}")

4.2 文档合并

def merge_documents(self):
    files, _ = QFileDialog.getOpenFileNames(
        self, "选择要合并的Word文档", "", 
        "Word文档 (*.docx)"
    )
    
    if len(files) < 2:
        QMessageBox.warning(self, "警告", "请至少选择两个文档进行合并")
        return
    
    try:
        merged = Document()
        for file in files:
            doc = Document(file)
            for element in doc.element.body:
                merged.element.body.append(element)
        
        save_path, _ = QFileDialog.getSaveFileName(
            self, "保存合并后的文档", "", 
            "Word文档 (*.docx)"
        )
        
        if save_path:
            if not save_path.endswith('.docx'):
                save_path += '.docx'
            merged.save(save_path)
            QMessageBox.information(self, "成功", f"文档已合并保存到: {save_path}")
    
    except Exception as e:
        QMessageBox.critical(self, "错误", f"合并失败:\n{str(e)}")

5. 部署为独立应用

可以使用PyInstaller将应用打包为独立可执行文件:

pip install pyinstaller
pyinstaller --onefile --windowed word_editor.py

这将在dist目录下生成可执行文件,可以在没有Python环境的电脑上运行。

6. 总结

这个Word编辑器实现了以下功能:

  • 基本的文档创建、打开、保存和另存为

  • 文本格式设置(字体、字号、加粗、斜体、下划线)

  • 图片和表格插入

  • 页眉页脚支持

  • 文档格式转换(如转PDF)

  • 文档合并功能

进一步扩展功能,例如:

  1. 添加更多格式选项(文本颜色、背景色、对齐方式等)

  2. 实现目录生成功能

  3. 添加批注和修订功能

  4. 实现邮件合并功能

  5. 添加文档加密/解密功能

  6. 集成拼写检查功能

使用Python读取和操作Word文件 Word文件是一种常见的文档格式,广泛用于编写文档、报告和其他类型的文本内容。在编程中,我们经常需要读取和操作Word文件,以便自动化处理文档内容。在本文中,我将介绍如何使用Python编程语言读取和操作Word文件。除了读取文本内容,python-docx库还提供了其他丰富的功能,比如创建新的Word文件、修改文档样式、插入表格和图片等。首先,我们需要安装python-docx库,它是一个用于读取和操作Word文件的强大工具。上述示例代码展示了一些常见的操作,您可以根据具体需求进行进一步的扩展和修改。 阅读详情

相关推荐

使用Python编程在本地打开OneDrive上分享的Word链接

首先,我们需要从OneDrive获取文件的共享链接。在本文中,我们将探讨如何使用Python编程语言来打开OneDrive上分享的Word文档链接。库来操作下载的Word文档。这个库提供了一些方便的方法来读取和修改Word文档的内容。通过以上代码,我们可以从OneDrive上的分享链接下载Word文档,并使用。库发送GET请求来获取文件的内容,并将其保存到本地文件。属性遍历文档中的段落,并打印每个段落的文本内容。是OneDrive上分享的Word文档链接,,它是下载的Word文档的本地路径。

2301_79326930的博客 554

Python+docx实现python对word文档的编辑

利用python来实现word批量编辑和修改,利用python来实现高效办公!

qq_39330735的博客 5787

半桥LLC谐振变换器及同步整流MATLAB仿真(二)

接上文,半桥LLC谐振变换器及同步整流MATLAB仿真

qq_55203246的博客 4341

用Python编写的Word一键排版工具

今天就给大家分享一款自己编写的Word文档一键排版软件,可以轻松帮助我们完成Word文档公文排版,即使你在电脑方面是小白,也可以一键搞定。从此,公文排版,再也不用求人啦,办公的文秘人员也可以腾出更多时间和精力去从事其他工作。

longefieder的博客 7683

对称式目镜设计实例

对称式目镜设计实例

weixin_72050316的博客 1165

华为公司职位分析:财经数据管理专员

本文依据华为官方财经数据管理专员岗位职责、岗位要求撰写,区别于助理岗,本岗位侧重独立主导、体系搭建、方案全权设计、项目闭环落地、数据资产价值运营,贴合华为财经数字化、数据底座建设、财务数据治理核心体系,全面拆解岗位专业知识、核心技能、简历打法与高频面试应答。

王国平的博客 167

python word操作_用 Python 操作 Word 文档

图片来源于网络♚作者:贠云龙Blog:zhihu.com/people/yunlongyun本文主要讲解python中操作word的思路。一、Hello,world!使用win32com需要安装pypiwin32pipinstallpypiwin32推荐使用python的IDLE,交互方便1、如何新建文档fromwin32com.clientimportDispatchapp...

weixin_39685836的博客 1256

【STM32 Blue Pill编程】-STM32CubeIDE开发环境搭建与点亮LED

本文为使用 STM32Cube IDE 的 STM32 Blue Pill 入门教程。 在本文中,我们将介绍如何控制 STM32 Blue Pill 的 GPIO 引脚。以及开发环境搭建。 本系列文章将重点介绍采用 ARM Cortex-M 的 BluepellSTM32F103C8 微控制器以及如何使用 STM32CubeIDE 对其进行编程。 STM32CubeIDE是STM32官网提供的IDE

视觉与物联智能 1080

python图形化编辑word_python word编辑器

python word编辑器# !/usr/bin/env Python# -*- coding:utf-8 -*-# Word.py# author: 董礼from docx import Documentfrom docx.enum.style import WD_STYLE_TYPEfrom docx.enum.text import WD_ALIGN_PARAGRAPHimport rec...

weixin_39850143的博客 562

redhawk:使用rh_setup.pl产生gsr模板

必须加的option工具会在rh_setup.pl给出提示,按照要求填写即可,另外工具会把所有填写的输入文件和信息放到rh_setup.init文件中,拷贝这个文件可在任何平级运行目录下,直接rh_setup.pl产生gsr以及run sta/dyn/em的脚本。rhsetup.pl -top top_cell_name -vdd_nets VDD 1.2 VDD2 1.2 -vss_nets VSS -frequency 100e6 (100m是默认值)更多学习内容请关注「拾陆楼」知识星球。

m0_61544122的博客 1073

Oracle EBS Interface/API(49)- AP付款取消API

Oracle EBS AP付款取消API介绍和简单实例

陈显平的专栏-Oracle EBS 854

用Python操作Word文档,看这一篇就对了!

输入selection并回车,发现成员一列中完全匹配Selection的只有4个类,这表示只有这些类的Selection属性可以返回Selection对象(如图)。光标焦点和选择范围在Word中,都是Selection。什么都没选择的光标焦点,和选择了整片文章的选择范围,代表了Selection的最小和最大范围。Styles的返回参数,标题1、标题2、标题3分别为-2、-3、-4,页眉为-32,标题为-63,其他见Styles文档。Selection是Word对象模型中的类,此处的s是它的对象(实例)。

aobulaien001的博客 1121

SAP-ABAP:SAP信贷管理核心:CL_UKM_BUPA_FACTORY类完整解析与实践指南

SAP信贷管理核心类CL_UKM_BUPA_FACTORY解析 CL_UKM_BUPA_FACTORY是SAP信贷管理(UKM)模块的核心类,作为业务伙伴信贷数据的中央控制台,它提供: 统一入口:标准化实例化客户/供应商信贷对象 主要功能: 获取单条/批量业务伙伴数据 安全保存数据变更 封装底层数据访问逻辑 典型应用场景: 信贷主数据批量维护 自动化限额调整 订单信贷实时检查 性能优化建议: 使用批量方法替代循环单条处理 合理设置字段选择 实施缓存策略 该类通过简洁API简化复杂信贷操作,是SAP信贷管理开

baidu_35680696的博客 1217

Python-docx文档自动化:从Word操作到Open XML编程

Word文档自动化是现代办公提效的核心能力,其本质是将非结构化文档转化为可编程的数据载体。基于Open XML标准,Python-docx绕过Word界面直接解析ZIP包内的XML文件,实现段落、表格、样式、页眉页脚等元素的精准控制。这种底层文本工程能力,赋予开发者对文档结构的完全掌控力,支撑批量生成、动态模板、条件渲染等高价值场景。相比VBA宏或UI模拟,它更稳定、可测试、易集成,尤其适合与Pandas、Matplotlib、Jinja2等工具协同构建端到端报告系统。本文聚焦Python-docx的真实落

weixin_32288959的博客 183

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Python编程实战 - Python实用工具与库 - 操作Word:python-docx

使用创建、修改、读取 Word 文档。向文档中添加标题、段落、图片、表格与样式。批量生成 Word 报告的自动化技巧。的强大之处在于它能让你将文档处理与业务逻辑无缝结合,为办公自动化、报告生成、合同生成等场景提供极大便利。

tenkofx的博客 595

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

python批量操作word文档实战_Python编程实战:根据数据表格,快速批量生成符合规范的WORD文档...

也许你会遇到一个需求,需要批量制作给很多人的信,这些信的内容结构上都是一样的,只有一些细节是不一样的,那么你该怎么完成这个需求? 是一个个复制word内容吗?实际案例这里给大家介绍一个强大的功能,通过使用word的mergedField功能生成规范的word模板,在这里,给大家举个例子。我们的例子是个很贴近收物业费的例子,我们从excel中都能看到一个小区的业主姓名、单元号、楼层号、面积、物业费等...

weixin_39585617的博客 471

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

python处理word_利用python批量处理Word文件——正文、标题

前面写了如何处理word中的表格:利用python批量处理Word文件——表格 ,其实我觉得word正文应该很少用到批处理,不过万一有朋友需要呢,我们要处理的文件内容千奇百怪,但我觉得难点其实是如何对我们想处理的内容进行读写,所以这篇博客的主要内容就是word文件的读写。我把word文件中的内容分为三类:一是表格;二是图片;三是其它内容。表格和图片的处理我在另外的博客中有详细说明,这里主要谈谈wo...

weixin_39974958的博客 885

Word文字批量替换+文件名批量修改 详细教程

本文介绍了两种批量处理Word文档的方法:VBA宏和Python脚本。VBA宏方法通过开发工具启用宏功能,插入模块代码实现批量替换文档内容并自动保存。Python脚本方法使用pywin32库,通过正则表达式批量修改文件名中的指定文本。两种方法都支持自定义查找/替换内容,适用于批量处理多个文档,并包含错误处理和操作结果提示。VBA方法适合处理文档内容替换,Python方法适合批量重命名文件,二者均可提高办公效率。

tingreal的博客 475

python三剑客安装_python编程操作office三剑客之word篇

安装包安装pip install python-docx -i https://pypi.doubanio.com/simple/引用import docx官方文档https://python-docx.readthedocs.io/en/latest/word基本概念一个word文件,整体上是一个线性的结构。最开头是文章标题,接下来是章节标题、一段一段的正文,有序列表,无序列表是两种特殊的正文;...

weixin_35122299的博客 221

Word转PDF三种实现方式全解析:手工、命令行与编程接口选型指南

文档格式转换是办公自动化与系统集成中的基础能力,其核心在于将可编辑文档冻结为固定版式,确保跨环境排版一致。实现原理主要依赖渲染引擎对文档元素进行解析与重排,技术价值体现在提升文档流转效率、保障归档合规性以及支撑在线预览与签署等业务场景。在批量处理、服务器端自动化或SaaS平台集成等需求下,Word转PDF的选型需综合考量文件量、运行环境与集成深度。命令行工具凭借批量脚本能力成为性价比之选,而编程接口则满足系统级嵌入与并发控制要求,三者共同覆盖从零散办公到产品化服务的完整光谱。

weixin_28681255的博客 85

惊艳!Python竟然可以这样玩转Word!超详细实例代码,让你秒变办公达人!

接下来,我将为大家带来超详细的实例代码讲解,让你轻松学会用Python操作Word,秒变办公达人!朋友们,通过上面的实例代码讲解,相信大家对Python操作Word已经有了初步的了解。最后,如果你对python语言还有任何疑问或者需要进一步的帮助,请访问https://www.xin3721.com 本站原创,转载请注明出处:https://www.xin3721.com/Python/python48994.html。这个库提供了丰富的API,让我们可以像操作Python对象一样轻松操作Word文档。

Oona_01的博客 203

教你怎么使用Python对word文档进行操作

使用Python对word文档进行操作 一、安装Python-docx Python-docx是专门针对于word文档的一个模块,只能读取docx 不能读取doc文件。说白了,python就相当于windows操作系统,QQ就是跑在windows操作系统上的软件,QQ最大的作用是可以去聊天,说话。 在这里python-docx就相当于这里的qq 它主要的功能是对docx文件进行操作,管理等.(个...

Python追梦 7万+

Python Word 操作全面指南

本文介绍了 Python Word 基础操作,包括安装 Python Word 库、读取 Word 文件、写入 Word 文件和修改 Word 文件。这些操作是 Python Word 操作的基础,掌握了这些操作,就可以进一步学习更高级的 Python Word 操作了。在下一部分中,我们将介绍如何使用 Python Word 库进行 Word 文档的批量处理。本部分介绍了如何使用 Python 对多个 Word 文件进行批量处理,包括读取、写入和修改。

极客代码 5862

Python操作word基础

在办公自动化的操作,往往可能会存在Word的操作。因为在办公时,对文档的操作是必不可少的。而在Python操作Word时,可以使用一个三方的模块库Python-docx安装常用方法与属性。

m0_64336780的博客 1万+

【实用篇】Python操作word

在日常工作中,有很多简单重复的劳动其实完全可以交给Python程序,比如根据样板文件(模板文件)批量的生成很多个Word文件或PowerPoint文件。Word是微软公司开发的文字处理程序,相信大家都不陌生,日常办公中很多正式的文档都是用Word进行撰写和编辑的,目前使用的Word文件后缀名一般为.docx 。PowerPoint是微软公司开发的演示文稿程序,是微软的Office系列软件中的一员,被商业人士、教师、学生等群体广泛使用,通常也将其称之为“幻灯片”。...

Z.z.z的博客 1万+
上一篇: Python编程:Excel操作
下一篇: Python编程:PPT操作
倔强老吕
博客等级 码龄12年 1909粉丝 352原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值