② 添加文字块儿
from docx import Document doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") # 这里相当于输入了一个空格,后面等待着文字输入 paragraph3 = doc.add_paragraph() paragraph3.add_run("我被加粗了文字块儿").bold = True paragraph3.add_run(",我是普通文字块儿,") paragraph3.add_run("我是斜体文字块儿").italic = True doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx")
结果如下:
③ 添加一个分页
from docx import Document doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") doc.add_page_break() doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx")
结果如下:
④ 添加图片
from docx import Document from docx.shared import Cm doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") doc.add_picture(r"G:\6Tipdm\7python办公自动化\concat_word\sun_wu_kong.png",width=Cm(5),height=Cm(5)) doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") """ Cm模块,用于设定图片尺寸大小 """
结果如下:
⑤ 添加表格
from docx import Document doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") list1 = [ ["姓名","性别","家庭地址"], ["唐僧","男","湖北省"], ["孙悟空","男","北京市"], ["猪八戒","男","广东省"], ["沙和尚","男","湖南省"] ] list2 = [ ["姓名","性别","家庭地址"], ["貂蝉","女","河北省"], ["杨贵妃","女","贵州省"], ["西施","女","山东省"] ] table1 = doc.add_table(rows=5,cols=3) for row in range(5): cells = table1.rows[row].cells for col in range(3): cells[col].text = str(list1[row][col]) doc.add_paragraph("-----------------------------------------------------------") table2 = doc.add_table(rows=4,cols=3) for row in range(4): cells = table2.rows[row].cells for col in range(3): cells[col].text = str(list2[row][col]) doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx")
结果如下:
⑥ 提取word表格,并保存在excel中(很重要)
from docx import Document from openpyxl import Workbook doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test2.docx") t0 = doc.tables[0] workbook = Workbook() sheet = workbook.active for i in range(len(t0.rows)): list1 = [] for j in range(len(t0.columns)): list1.append(t0.cell(i,j).text) sheet.append(list1) workbook.save(filename = r"G:\6Tipdm\7python办公自动化\concat_word\来自word中的表.xlsx")
结果如下:
3、利用Python调整Word文档样式
1)修改文字字体样式
from docx import Document from docx.shared import Pt,RGBColor from docx.oxml.ns import qn doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test2.docx") for paragraph in doc.paragraphs: for run in paragraph.runs: run.font.bold = True run.font.italic = True run.font.underline = True run.font.strike = True run.font.shadow = True run.font.size = Pt(18) run.font.color.rgb = RGBColor(255,255,0) run.font.name = "宋体" # 设置像宋体这样的中文字体,必须添加下面2行代码 r = run._element.rPr.rFonts r.set(qn("w:eastAsia"),"宋体") doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\_test1.docx")
结果如下:
2)修改段落样式
① 对齐样式
from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") print(doc.paragraphs[0].text) doc.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER # 这里设置的是居中对齐 doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\对齐样式.docx") """ LEFT,CENTER,RIGHT,JUSTIFY,DISTRIBUTE,JUSTIFY_MED,JUSTIFY_HI,JUSTIFY_LOW,THAI_JUSTIFY """
结果如下:
② 行间距调整
from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH doc = Document(r"G:\6Tipdm\7python办公自动化\concat_word\test1.docx") for paragraph in doc.paragraphs: paragraph.paragraph_format.line_spacing = 5.0 doc.save(r"G:\6Tipdm\7python办公自动化\concat_word\行间距.docx")
结果如下:
③ 段前与段后间距
这里提供代码,自行下去检验