word编辑

#  coa coop
#pip install python-docx  
%cd D:\python全站\office
D:\python全站\office
%pwd
'D:\\python全站\\office'
# 文档--document
from docx import Document  # docx , Document
doc = Document()  #生成一个文档对象,一切皆对象
doc.add_heading('测试一下')
# doc.add_heading?
# Signature: doc.add_heading(text='', level=1)
doc.add_paragraph('coop word')  #添加段落
doc.add_paragraph('coop word')
doc.add_paragraph('coop word')
<docx.text.paragraph.Paragraph at 0x22c2b7c26a0>
doc.save('coop.docx')  
# docx 新的word格式,word打开的时候不能执行此步骤
doc.add_heading('这是一个标题', level = 0)
<docx.text.paragraph.Paragraph at 0x22c2b7c2588>
doc.save('coop.docx')
doc = Document()  # 内存中的对象
doc.add_heading('新的标题', level = 0)
<docx.text.paragraph.Paragraph at 0x22c2c7da748>
doc.add_paragraph('coop', 'Subtitle') #样式 subtitle
<docx.text.paragraph.Paragraph at 0x22c2c7da198>
doc.add_paragraph('正文正文正文正文正文正文正文')
<docx.text.paragraph.Paragraph at 0x22c2c7dab70>
from docx.enum.style import WD_STYLE_TYPE 
# enum枚举,定义好的全局变量
# 查看已有的样式
for i in doc.styles:
    if i.type == WD_STYLE_TYPE.PARAGRAPH: # 段落样式
#         print(i)
#         print(i.font)
        print(i.name)
Normal
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Heading 7
Heading 8
Heading 9
No Spacing
Title
Subtitle
List Paragraph
Body Text
Body Text 2
Body Text 3
List
List 2
List 3
List Bullet
List Bullet 2
List Bullet 3
List Number
List Number 2
List Number 3
List Continue
List Continue 2
List Continue 3
macro
Quote
Caption
Intense Quote
TOC Heading
for i in doc.styles:
    if i.type == WD_STYLE_TYPE.CHARACTER:  #字符样式
        print(i.name)
Default Paragraph Font
Heading 1 Char
Heading 2 Char
Heading 3 Char
Title Char
Subtitle Char
Body Text Char
Body Text 2 Char
Body Text 3 Char
Macro Text Char
Quote Char
Heading 4 Char
Heading 5 Char
Heading 6 Char
Heading 7 Char
Heading 8 Char
Heading 9 Char
Strong
Emphasis
Intense Quote Char
Subtle Emphasis
Intense Emphasis
Subtle Reference
Intense Reference
Book Title
# 给段落和文字添加样式
p = doc.add_paragraph('ccccccccc正文',style='Title')
p.add_run('333333run,run run') #段落后添加内容.add_run()
<docx.text.run.Run at 0x22c2c7daf28>
para = doc.add_paragraph('cccccccc又一个新的段落')
para.add_run('追加新的文本')
para.add_run('追加新的文本')
para.add_run('追加新的文本')
para.add_run('追加新的文本')
para.add_run('追加新的文本')
<docx.text.run.Run at 0x22c2c7df0f0>
# 添加对齐样式
from docx.enum.text import WD_ALIGN_PARAGRAPH
para.alignment = WD_ALIGN_PARAGRAPH.CENTER
para_left = doc.add_paragraph('左对齐的段落') #添加对象
para_left.alignment = WD_ALIGN_PARAGRAPH.LEFT  
#对上面的段落进行左对齐
left_run = para_left.add_run('单独调整这几个文字的样式')
from docx.shared import Pt # 对上面追加的字体进行字体的设置
left_run.font.bold = True # 加粗
left_run.font.size = Pt(25)  # 字号25
# 添加图像
doc.add_picture('logo-64x64.png')
<docx.shape.InlineShape at 0x22c2c7d9438>
doc.save('style and picture.docx')
table = doc.add_table(rows=10, cols=8)
cell = table.cell(2,3)
cell.text = 'coop'
table.cell(5,6).text = '888888888'
for x in doc.styles:
    if x.type == WD_STYLE_TYPE.TABLE:
        print(x.name)
Normal Table
Table Grid
Light Shading
Light Shading Accent 1
Light Shading Accent 2
Light Shading Accent 3
Light Shading Accent 4
Light Shading Accent 5
Light Shading Accent 6
Light List
Light List Accent 1
Light List Accent 2
Light List Accent 3
Light List Accent 4
Light List Accent 5
Light List Accent 6
Light Grid
Light Grid Accent 1
Light Grid Accent 2
Light Grid Accent 3
Light Grid Accent 4
Light Grid Accent 5
Light Grid Accent 6
Medium Shading 1
Medium Shading 1 Accent 1
Medium Shading 1 Accent 2
Medium Shading 1 Accent 3
Medium Shading 1 Accent 4
Medium Shading 1 Accent 5
Medium Shading 1 Accent 6
Medium Shading 2
Medium Shading 2 Accent 1
Medium Shading 2 Accent 2
Medium Shading 2 Accent 3
Medium Shading 2 Accent 4
Medium Shading 2 Accent 5
Medium Shading 2 Accent 6
Medium List 1
Medium List 1 Accent 1
Medium List 1 Accent 2
Medium List 1 Accent 3
Medium List 1 Accent 4
Medium List 1 Accent 5
Medium List 1 Accent 6
Medium List 2
Medium List 2 Accent 1
Medium List 2 Accent 2
Medium List 2 Accent 3
Medium List 2 Accent 4
Medium List 2 Accent 5
Medium List 2 Accent 6
Medium Grid 1
Medium Grid 1 Accent 1
Medium Grid 1 Accent 2
Medium Grid 1 Accent 3
Medium Grid 1 Accent 4
Medium Grid 1 Accent 5
Medium Grid 1 Accent 6
Medium Grid 2
Medium Grid 2 Accent 1
Medium Grid 2 Accent 2
Medium Grid 2 Accent 3
Medium Grid 2 Accent 4
Medium Grid 2 Accent 5
Medium Grid 2 Accent 6
Medium Grid 3
Medium Grid 3 Accent 1
Medium Grid 3 Accent 2
Medium Grid 3 Accent 3
Medium Grid 3 Accent 4
Medium Grid 3 Accent 5
Medium Grid 3 Accent 6
Dark List
Dark List Accent 1
Dark List Accent 2
Dark List Accent 3
Dark List Accent 4
Dark List Accent 5
Dark List Accent 6
Colorful Shading
Colorful Shading Accent 1
Colorful Shading Accent 2
Colorful Shading Accent 3
Colorful Shading Accent 4
Colorful Shading Accent 5
Colorful Shading Accent 6
Colorful List
Colorful List Accent 1
Colorful List Accent 2
Colorful List Accent 3
Colorful List Accent 4
Colorful List Accent 5
Colorful List Accent 6
Colorful Grid
Colorful Grid Accent 1
Colorful Grid Accent 2
Colorful Grid Accent 3
Colorful Grid Accent 4
Colorful Grid Accent 5
Colorful Grid Accent 6
table.style = 'Light Grid Accent 3'
doc.save('table.docx')

猜你喜欢

转载自blog.51cto.com/13118411/2146980