问题描述
我想编写一个程序,从Word文档中复制文本并将其粘贴到另一个文档。我正在尝试使用 python-docx 库来做到这一点。我能够使用以下代码执行此操作,但它不会复制粗体,斜体,带下划线,也不会复制彩色部分,因为它们和只有他们的文字:
I want to write a program that copies text from a Word document and pastes it to another. I'm trying to do that using the python-docx library. I was able to do that with the following code, but it does not copy the bold, italic, underlined nor colored parts as they are and only their text:
from docx import Document
input = Document('SomeDoc.docx')
paragraphs = []
for para in input.paragraphs:
p = para.text
paragraphs.append(p)
output = Document()
for item in paragraphs:
output.add_paragraph(item)
output.save('OutputDoc.docx')
什么我试过了:
我试过直接复制段
对象进入输出文档,但它也不起作用:
What I have tried:
I've tried copying the paragraph
object directly into the output document, but it doesn't work either:
from docx import Document
input = Document('SomeDoc.docx')
output = Document()
for para in input.paragraphs:
output.add_paragraph(para)
output.save('OutputDoc.docx')
推荐答案
def get_para_data(output_doc_name, paragraph):
"""
Write the run to the new file and then set its font, bold, alignment, color etc. data.
"""
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
1.在文件中添加一个新的段
对象。
2.添加一个新的运行
到该段。
3.检查每个样式粗体,斜体和下划线是 True
, False
,无
。如果它是 True
,运行
将采用该样式,如果它是 False
,它不会是那种风格,如果它是无
,它将由它所在的段落的默认样式继承。然后它应用样式到运行
。
3.检查 RGB 中运行的颜色是什么,并将找到的颜色应用于运行
。
4.检查运行的字体是什么,并将找到的字体应用于运行
。
5.检查运行的对齐方式,并将找到的对齐设置应用于运行
。
您需要为其提供输出文档的名称和要复制的段落。
例如:
1. Adds a new paragraph
object to the file.
2. Adds a new run
to that paragraph.
3. Checks whether each of the styles bold, italic and underline is True
, False
, None
. If it's True
, the run
will be in that style, if it's False
, it won't be in that style, and if it's None
, it will be inherited by the default style of the paragraph it's in. Then it applies the styles to the run
.
3. Checks what's the color of the run in RGB and applies the found color to the run
.
4. Checks what's the font of the run and applies the found font to the run
.
5. Checks what's the alignment of the run and applies the found alignment setting to the run
.
You need to give it the name you gave your output document and the paragraphs you want to copy.
For Example:
# Imports
input_doc = Document('InputDoc.docx')
output_doc = Document()
# Call the function
get_para_data(output_doc, input_doc.paragraphs[3])
# Save the new file
output_doc.save('OutputDoc.docx')
如果你'我想复制整个文件,我建议你这样做:
If you'd like to copy the entire document I suggest you do this:
for para in input_doc.paragraphs:
get_para_data(output_doc, para)
output_doc.save('OutputDoc.docx')
这篇关于如何复制word文档的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!