1. 程式人生 > >word編輯

word編輯

fault pic pytho text str asi light line 枚舉

# 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‘)

word編輯