简体   繁体   English

如何使用 python docx 保留所有 styles 和对齐方式克隆/复制.docx 文件

[英]How to clone/copy .docx file with keeping all styles and alignments using python docx

I want to clone docx file with all of its styles and alignments.我想克隆 docx 文件及其所有 styles 和对齐。 This is my code:这是我的代码:

import docx
from docx import Document

doc = docx.Document('my_word_file.docx')
allText = []
for docpara in doc.paragraphs:
    allText.append((docpara.text).replace('a' ,'s')

mydoc = docx.Document()
mydoc.add_paragraph(allText)
mydoc.save("cloned.docx")

So I am fixing some mistakes and file is saving, but without styles or tabs or enters.所以我正在修复一些错误并且文件正在保存,但没有 styles 或制表符或输入。 How to keep changes and stylings too?如何保持变化和样式呢?

I think that the way is to write your own function to copy the text and style of each paragraph.我觉得方法是自己写function来复制每个段落的文字和样式。 Here is a first implementation of what you could do:这是您可以做的第一个实现:

from docx import Document

def copy_paragraph(output_doc, paragraph):

    output_paragraph = output_doc.add_paragraph()
    # Alignment data of whole paragraph
    output_paragraph.paragraph_format.alignment = paragraph.paragraph_format.alignment
    for row in paragraph.runs:
        output_row = output_paragraph.add_run(row.text)
        # Font data
        output_row.style.name = row.style.name
        # Size of font data
        output_row.font.size = row.font.size
        # Bold data
        output_row.bold = row.bold
        # Italic data
        output_row.italic = row.italic
        # Underline data
        output_row.underline = row.underline
        # Color data
        output_row.font.color.rgb = row.font.color.rgb
    
doc = Document('my_word_file.docx')
mydoc = Document()
for paragraph in doc.paragraphs:
    copy_paragraph(mydoc, paragraph)
mydoc.save("cloned.docx")

I don't exclude that there may be other parameters that need to be copied (for example the highlighting of the text, etc.).我不排除可能还有其他参数需要复制(例如文本的突出显示等)。 In this case I suggest you to do some tests and consult the docx documentation that is pretty well written.在这种情况下,我建议您进行一些测试并查阅写得很好的docx 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python docx 遍历 docx 文件中的所有文本元素? - How to iterate over all Text Elements in a docx file with Python docx? 使用 python 提取 docx 文件中的所有图像 - Extract all the images in a docx file using python 如何使用python-docx在docx文件中写入多个表? - How to write multiple tables in docx file using python-docx? 如何替换 .docx 文件中的多个单词并使用 python-docx 保存 docx 文件 - How to replace multiple words in .docx file and save the docx file using python-docx 使用Python以编程方式合并两个docx文件,将字符样式保留在合并的段落中 - Programmatically merge two docx file with Python, keeping the character styles within the merged paragraph 如何在python docx中更改句子的字体样式 - How to change font styles in python docx for a sentence 有没有办法使用 python-docx 将所有项目符号列表替换为 docx 文件中的编号列表? - Is there a way to replace all the bullet lists with numbered list in docx file using python-docx? 如何在 python 中使用 docx 或 python-docx package 将多个 plotly 图保存在一个文档文件中 - How to save multiple plotly graphs in one document file using docx or python-docx package in python 如何使用 python-docx 更改所有 .docx 文档的字体大小 - How to change font size of all .docx document with python-docx 使用python-docx在docx文件中突出显示段落 - highlight paragraph in docx file using python-docx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM