【问题标题】:accessing/modifying Table of contents of a docx through python通过python访问/修改docx的目录
【发布时间】:2022-08-22 13:18:19
【问题描述】:

我需要更改 MS-Word 文档中 TOC 内容的字体样式。 这是我想通过 python 脚本自动完成的。

我能够访问 TOC 表,我也可以对其进行迭代......但无法访问内容的字体和大小。 我的代码是用 Python 编写的。有人可以帮忙吗。我想将字体/大小更改为特定的字体/大小。

请帮助如何做到这一点: 这是我的代码:

from pydoc import doc
from docx.api import Document
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from datetime import date
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import OxmlElement, ns
from docx.oxml.shared import OxmlElement # Necessary Import
from docx.oxml.shared import qn
from docx.oxml.ns import qn
from docx.text.paragraph import Paragraph
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
from docx2python import docx2python
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from pathlib import Path
import diff_match_patch
from docx.enum.text import WD_BREAK
from docx.text.run import Run
from docx.oxml.text.run import CT_R
from docx.enum.style import WD_STYLE
import re
import mmap
import win32com.client
import inspect, os
import calendar
import tkinter as tk
from tkinter import filedialog
import time
import lxml
from calendar import month_name
from docx.enum.text import WD_COLOR_INDEX
from docx.enum.text import WD_LINE_SPACING
from docx.shared import RGBColor
import docx

root = tk.Tk()
root.withdraw()

file = filedialog.askopenfilename()
filepath = os.path.dirname(os.path.abspath(file))
document = Document(file)

def tocM():
    body_elements = document._body._body
    rs = body_elements.xpath(\'.//w:r\')
    table_of_content = [r for r in rs if r.style == \"Hyperlink\"]
    for i in table_of_content:
        print(\"1111\");
        print(i.text)
        print(i)
        print(\"2222\");
    return;


tocM()

我附上了我正在处理的 docx 文件的快照。 请帮忙。

问候

    标签: python ms-word docx


    【解决方案1】:

    您是否找到任何解决方案或任何其他方式来实现?

    【讨论】:

    猜你喜欢
    • 2015-09-23
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    相关资源
    最近更新 更多