您现在的位置是:网站首页> 编程资料编程资料

如何使用python docx模块操作word文档_python_

2023-05-26 363人已围观

简介 如何使用python docx模块操作word文档_python_

引言

入门python-docx很容易。让我们来看一下基础知识。

官方文档

打开文档

你需要的第一件事是工作的文档。最简单的方法是:

from docx import Document document = Document()

这将打开一个基于默认“模板”的空白文档,您可以打开并使用现有的Word文档的工作python-docx,我们会让事情变得简单。

正文应用字符样式(字体,大小,颜色)

# 设置正文字型 英文字型:Times New Roman; 中文字型:宋体 document.styles['Normal'].font.name = 'Times New Roman' document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') # 设置正文字体大小 document.styles['Normal'].font.size = Pt(12) # 设置正文字体颜色 document.styles['Normal'].font.color.rgb = RGBColor(0, 0, 0)

添加标题

document.add_heading('The REAL meaning of the universe')

默认情况下,这会添加顶级标题,Word中显示为“标题1”。当您需要子节的标题时,只需指定所需的级别为1到9之间的整数:

document.add_heading('The role of dolphins', level=2)

level代表word文档内的标题等级,从0开始。

操作段落

添加段落

段落是Word的基础。它们用于正文文本,但也用于标题和列表项目(如项目符号)。

这里是添加一个最简单的方法:

paragraph_format= document.add_paragraph('床前明月光\n疑是地上霜\n举头望明月\n低头思故乡', style='') paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #文本居中 #左缩进 from docx.shared import Inches paragraph_format.left_indent = Inches(0.3) #首行缩进 paragraph_format.first_line_indent = Inches(0.3) #上行间距 from docx.shared import Pt paragraph_format.space_before = Pt(18) #下行间距 paragraph_format.space_after = Pt(12) #增加分页 document.add_page_break()

style设置段落样式,有两种类型:

  • ①List Bullet:项目符号
  • ②List Number:列表编号

此方法返回对段落的引用,新添加的段落在文档的结尾

还可以使用一个段落作为“光标”,并在其上直接插入一个新段落

prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')

删除段落

python-docx中并没有提供delete()方法, github上给出了解决方法:

def delete_paragraph(paragraph): p = paragraph._element p.getparent().remove(p) # p._p = p._element = None paragraph._p = paragraph._element = None

经试验, 此方法对删除段落,表格,标题, 图片都是管用的:

from docx import Document docx = Document('word_file.docx') def delete_docx_prefix_description(docx): delete_paragraph(docx.tables[0]) # 删除word中第一个table for p in docx.paragraphs: delete_paragraph(p) if ''.join(p.text.split(' ')).lower()=='header_keyword': break for p in docx.paragraphs: if p.text.lower()=='': # 删除word中在开始部分的空白段落 delete_paragraph(p) else: break

替换文字

# 将想要替换的内容写成字典的形式, # dict = {"想要被替换的字符串": "新的字符串"} replace_dict = { "苹果":"apple", "香蕉":"banana", "猕猴桃":"Kiwi fruit", "火龙果":"pitaya", }
def check_and_change(document, replace_dict): """ 遍历word中的所有 paragraphs,在每一段中发现含有key 的内容,就替换为 value 。 (key 和 value 都是replace_dict中的键值对。) """ for para in document.paragraphs: for i in range(len(para.runs)): for key, value in replace_dict.items(): if key in para.runs[i].text: print(key+"->"+value) para.runs[i].text = para.runs[i].text.replace(key, value) return document

设置段落对齐方式

paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

这里使用了CENTER这个枚举值。

字体格式

这里引用一下官方文档的说明:

In order to understand how bold(粗体) and italic(斜体) work, you need to understand a little about what goes on inside a paragraph. The short version is this:

  • A paragraph holds all the block-level(块级元素) formatting, like indentation(缩进), line height, tabs, and so forth.
  • Character-level formatting, such as bold and italic, are applied at the run level(运行级别). All content within a paragraph must be within a run, but there can be more than one. So a paragraph with a bold word in the middle would need three runs, a normal one, a bold one containing the word, and another normal one for the text after.

When you add a paragraph by providing text to the method, it gets put into a single run. You can add more using the method on the paragraph:.add_paragraph().add_run()

为此需要设置第一次运行的正常文本,这个文本可以为空。

P = document.add_paragraph('') run = P.add_run("静夜思") run.font.color.rgb = RGBColor(255, 0, 0) run.font.size = Pt(14) run.bold = True run.italic = False P.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #加粗 paragraph.add_run(u'粗体').bold = True #斜体 paragraph.add_run(u'斜体、').italic = True #设置中文字体 paragraph.add_run(u'设置中文字体,') run.font.name=u'宋体' r = run._element r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') #字号 paragraph.add_run(u'设置字号').font.size=Pt(24) #增加引用 document.add_paragraph('Intense quote', style='Intense Quote') #增加有序列表 document.add_paragraph( u'有序列表元素1',style='List Number' ) document.add_paragraph( u'有序列别元素2',style='List Number' ) #增加无序列表 document.add_paragraph( u'无序列表元素1',style='List Bullet' ) document.add_paragraph( u'无序列表元素2',style='List Bullet' ) #增加图片 document.add_picture('jdb.jpg',width=Inches(1.25)) #增加表格 table = document.add_table(rows=3,cols=3) hdr_cells=table.rows[0].cells hdr_cells[0].text="第一列" hdr_cells[1].text="第二列" hdr_cells[2].text="第三列" hdr_cells = table.rows[1].cells hdr_cells[0].text = '2' hdr_cells[1].text = 'aerszvfdgx' hdr_cells[2].text = 'abdzfgxfdf' hdr_cells = table.rows[2].cells hdr_cells[0].text = '3' hdr_cells[1].text = 'cafdwvaef' hdr_cells[2].text = 'aabs zfgf'

添加分页符

你想要下一个文本在一个单独的页面

document.add_page_break()

添加表

一个经常遇到的内容,它自己的表格呈现,排列在整齐的行和列。

以下是添加表格的方法:

table = document.add_table(rows=2, cols=2)

表具有几个属性和方法,您将需要它们来填充它们。访问单个单元格可能是一个好的开始的地方。

作为基线,我们可以始终按其行和列指示访问单元格:

cell = table.cell(0, 1) cell.text = 'parrot, possibly dead'

通常,一次访问一行单元格更容易,例如,当从数据源填充可变长度的表时。在.rows 一个表中的属性提供给单独的行,每个都具有一个 .cells属性。该.cells两个物业Row和Column 支持索引访问,就像一个列表:

row = table.rows[1] row.cells[0].text = 'Foo bar to you.' row.cells[1].text = 'And a hearty foo bar to you too sir!'

.rows.columns桌子上的集合是可迭代的,这样你就可以直接在使用它们for循环。

相同的.cells上行或列序列:

for row in table.rows: for cell in row.cells: print(cell.text)

如果你想在表中的行或列的计数,只要使用len()的顺序:

row_count = len(table.rows) col_count = len(table.columns)

您还可以以递增方式向表中添加行,如下所示:

row = table.add_row()

这对于我们上面提到的可变长度表场景非常方便:

# get table data ------------- items = get_things_from_database_or_something() # add table ------------------ table = document.add_table(1, 3) # populate header row -------- heading_cells = table.rows[0].cells heading_cells[0].text = 'Qty' heading_cells[1].text = 'SKU' heading_cells[2].text = 'Description' # add a data row for each item for item in items: cells = table.add_row().cells cells[0].text = str(item.qty) cells[1].text = item.sku cells[2].text = item.desc

同样的工作对列,虽然我还没有看到它的一个用例。Word具有一组预格式化的表格样式,您可以从其表格样式库中选择。您可以将其中的一个应用于表格,

如下所示:

table.style = 'LightShading-Accent1'

通过从表样式名称中删除所有空格形成样式名称。通过将鼠标悬停在Word的表样式库中的缩略图上,可以找到表样式名称。

添加图片

Word中,您可以将图像使用的文档中的菜单项

-六神源码网