fromdocximportDocumentfromdocx.sharedimportPt, RGBColor# 设置像素、缩进等, 设置字体颜色fromdocx.oxml.nsimportqnfromdocx.enum.styleimportWD_STYLE_TYPEfromdocx.enum.textimportWD_ALIGN_PARAGRAPH# 导入段落对齐方式# 打开文档doc = Document("test.docx")# 添加样式style = doc.styles.add_style('tstyle',...
from docx import Document from docx.oxml.ns import qn from docx.enum.style import WD_STYLE_TYPE document = Document() # 直接设置中文字体,对中文无效 paragraph1 = document.add_paragraph() run = paragraph1.add_run('aBCDefg这是中文') font = run.font font.name = '宋体' # 方法1 直接修改...
from docx import Document # 打开一个已存在的Word文档 doc = Document('这是一个文档.docx') paragraph2 = doc.paragraphs[1] runs = paragraph2.runs for run in runs: print(run.text) 3、读取表格内容 for table in doc.tables: # 遍历表格的每一行 for row in table.rows: # 遍历行中的每一个...
run.underline = True #给内联设置下划线 paragraph.add_run('粗体').bold = True #文字和格式一起修改 #设置字体--麻烦一点 run.font.name=u'华文彩云' r = run._element r.rPr.rFonts.set(qn('w:eastAsia'), '华文彩云') #需要 from docx.oxml.ns imp...
fromdocximportDocument# 打开Word文档doc=Document('example.docx')# 获取第一个段落paragraph=doc.paragraphs[0]# 获取第一个段落中的第一个run对象run=paragraph.runs[0] 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 替换run对象中的内容 一旦我们获取了run对象,就可以通过修改它的text属性来替换其中的文本内容...
Document: 文档 - Paragraph:段落 - Run:文字块 Document:文档 - Table:表格 - Row/Column:行或者列 - Cell :单元格 4、获取word内容 导入word from docx import Document # 只要不指定路径,就默认为创建新Word文件 wordfile = Document(path) 获取段落以及文本内容 from docx import Document # 只要不指定路...
1、python - docx 数据结构 1)一般文档结构 整篇文档是一个Document,将正文分成若干个段落Paragraph(以回车算,一个回车一个段落,表格不算),每个段落Paragraph再划分为若干个run对象。每个run对象内的文本样式都是一致的,也就是说,在从docx文件生成文档对象时,python-docx会根据样式的变化来将文本切分为一个个的Run...
paragraph.add_run()run.add_bookmark_start('my_bookmark')run.text = '点击这里跳转'run.add_bookmark_end('my_bookmark')# 引用书签paragraph = document.add_paragraph()hyperlink = paragraph.add_run('点击这里跳转到书签位置')hyperlink.hyperlink = docx.shared.Hyperlink(docx.shared.DocxHyperlink(docx...
paragraph=doc.add_paragraph('This is a sample document created using the python-docx library.')run=paragraph.runs[0]run.bold=True run.italic=True # 添加标题 doc.add_heading('Section 1: Introduction',level=2)# 添加编号列表 list_paragraph=doc.add_paragraph()list_paragraph.add_run('Bullet 1...
document.add_paragraph('这是一个使用Python创建的Word文档自动化示例。') # 保存文档 document.save('example.docx') 您还可以使用python-docx库添加标题、列表、表格等内容。有关更多详细信息,请参考python-docx库的官方文档。 二、格式化文本和段落 除了添加内容,您还可以使用python-docx库格式化文本和段落。以下...