python table对象_(RPA学习):Python-docx 常用方法
原標題:(RPA學習):Python-docx 常用方法
**1.**引用庫
from docx import Document
**2.**新建一個空 docx
document = Document()
**3.**保存 docx
document.save(‘c:/test2.docx’)
**4.**打開指定的 docx
document = Document(‘c:/test.docx’)
**5.**在末尾增加一個段落 paragraph
paragraph = document.add_paragraph(‘Lorem ipsum dolor sit amet.’)
**6.**插入段落
使用一個段落作為“光標”,并在其上直接插入一個新段落:在段落 paragraph 前增加一個段落 prior_paragraph
prior_paragraph = paragraph.insert_paragraph_before(‘Lorem ipsum’)
**7.**添加標題
document.add_heading(‘The REAL meaning of the universe’)
增加一個級別為 2 的標題
document.add_heading(‘The role of dolphins’, level=2)
**8.**添加分頁符
document.add_page_break()
**9.**添加表
table = document.add_table(rows=2, cols=2)
訪問單元格:在 table 表的 0 行 1 列
cell = table.cell(0, 1)
設置單元格文本
cell.text = ‘parrot, possibly dead’
訪問 table 表整行
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!’
for row in table.rows:
for cell in row.cells:
print(cell.text)
獲取表格行與列數
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 = ‘Deion’
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
預格式化表格樣式
table.style = ‘LightShading-Accent1’
**10.**添加圖片
document.add_picture(‘image-filename.png’)
圖像大小設置
from docx.shared import Inches
document.add_picture(‘image-filename.png’, width=Inches(1.0))
**11.**應用段落樣式
創建段落時定義樣式
document.add_paragraph(‘Lorem ipsum dolor sit amet.’, style=‘ListBullet’)
創建段落時修改樣式
paragraph = document.add_paragraph(‘Lorem ipsum dolor sit amet.’)
paragraph.style = ‘ListBullet’
**12.**應用粗體和斜體
中間有一個粗體字的段落需要三個段落,一個正常的段落,一個包含該單詞的粗體,以及另一個正常的段落
paragraph = document.add_paragraph(’Lorem ipsum ’)
paragraph.add_run(‘dolor sit amet.’)
#Run 對象既有.bold 和.italic 屬性
paragraph = document.add_paragraph(’Lorem ipsum ’)
run = paragraph.add_run(‘dolor’)
run.bold = True
paragraph.add_run(’ sit amet.’)
可以對結果集粗體或斜體正確的.add_run()
paragraph.add_run(‘dolor’).bold = True
is equivalent to:
run = paragraph.add_run(‘dolor’)
run.bold = True
except you don’t have a reference to run afterward
它不是必須提供的文字給.add_paragraph() 方法。這可以使你的代碼更簡單
paragraph = document.add_paragraph()
paragraph.add_run(’Lorem ipsum ’)
paragraph.add_run(‘dolor’).bold = True
paragraph.add_run(’ sit amet.’)
**13.**應用字符樣式
可使用 add_run 創建段落時指定字符樣式
paragraph = document.add_paragraph(’Normal text, ’)
paragraph.add_run(‘text with emphasis.’,‘Emphasis’)
也可以創建段落后指定字符樣式
paragraph = document.add_paragraph(’Normal text, ’)
run = paragraph.add_run(‘text with emphasis.’)
run.style = ‘Emphasis’
**14.**讀寫 docx 文件
新建或打開文件,若指定路徑則是打開文檔;若沒有指定路徑則是新建文檔
#引用庫
import docx
#新建文檔
doc_new = docx.Document()
#讀取文檔
doc = docx.Document(ur’C:\1.docx’)
#保存文檔
doc.save(path_or_stream)
對象集合
python-docx 包含了 word 文檔的相關對象集合。
#段落集合
doc.paragraphs
#表格集合
doc.tables
#節 集合
doc.sections
#樣式集合
doc.styles
#內置圖形
doc.inline_shapes
插入段落
doc.add_paragraph(u’第一段’,style=None) #插入一個段落,文本為“第一段”
#默認是不應用樣式,這里也可以不寫 style 參數,或者指定一個段落樣式
doc.add_paragraph(u’第二段’,style=‘Heading
2’)
#這些樣式都是 word 默認帶有的樣式,可以直接羅列出來有哪些段落樣式
新增樣式
from docx import Document
from docx.shared import RGBColor #這個是 docx 的顏色類
#新建文檔
doc = Document()
#新增樣式 (第一個參數是樣式名稱,第二個參數是樣式類型:1 代表段落;2 代表字符;3 代表表格)
style = doc.styles.add_style(‘style name 1’, 2)
#設置具體樣式 (修改樣式字體為藍色,當然還可以修改其他的,大家自己嘗試)
style.font.color.rgb = RGBColor(0x0, 0x0, 0xff)
應用字符樣式
字符自然是在段落里面的,可以采用下面方法給段落追加文字和設置字符樣式
#插入一個空白段落
p = doc.add_paragraph('')
p.add_run(‘123’, style=“Heading 1 Char”)
p.add_run(‘456’)
p.add_run(‘789’, style=“Heading 2 Char”)
#這樣一個段落就應用了兩個字符樣式,中間“456”就沒應用樣式
print p.text # 輸出結果是 u’123456789’ 也還是連續的
設置字體。
當然可以不用通過設置樣式對某些字進行設置,也可以直接設置。
p = doc.add_paragraph('')
r = p.add_run(‘123’)
r.font.bold = True # 加粗
r.font.italic = True # 傾斜 等等…
8、表格操作。表格也是經常用到的一種對象類型。
#新建一個 2x3 的表格,style 可以不寫
table=doc.add_table(rows=2,cols=3,style=None)
#可以用 table 的 rows 和 columns 得到這個表格的行數和列數
print len(table.rows)
print len(table.columns)
#遍歷表格
for row in table.rows:
row.cells[0].text = ‘1’
#print row.cells[0].text
#新增行或列
table.add_row()
table.add_column()
**15.**獲取段落文本
doc = Document(r’C:\test.docx’)
#變量 doc 為 docx.document.Document 類型
#doc.paragraphs 為 list 類型
for i in doc.paragraphs:
temp_str=i.text
#變量 temp_str 為 str 類型
print(temp_str)返回搜狐,查看更多
責任編輯:
總結
以上是生活随笔為你收集整理的python table对象_(RPA学习):Python-docx 常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 民生银行信用卡额度一般是多少
- 下一篇: 2020年发行哪些纪念币?千万别错过!