程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python docx] add tables, merge cells, and set styles

編輯:Python

1. form

1.1 Add table

Adding tables is simple , Just call add_table() that will do , Return to one Table object , The parameter can specify the row 、 Column 、 style

from docx import Document
doc = Document()
# Add one 5 That's ok 3 List of tables , The style is grid solid 
table = doc.add_table(5, 3, )
doc.save('./test.docx')
1.2 Add ranks
from docx import Document
from docx.shared import Cm, RGBColor, Pt
...
table.add_row() # Add a line at the bottom 
table.add_column(Pt(25)) # Add a column on the far right and specify a width of 25 pounds 
1.3 Table style
...
table.cell(1, 2).text = " Cold hope "
table.style.font.size = Pt(15) # font size 15 pounds 
table.style.font.color.rgb = RGBColor.from_string("6495ED") # The font color 
table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT # Align left 

2. Row and column object

The first is a table (Table), There are rows in the table (Row) And column (Column), There are cells in rows or columns (Cell)
python-docx of use _Row and _Column Represent rows and columns respectively ,, use _Rows and _Columns Represents multiple rows and columns , have access to Table Object's rows and columns Property to get all rows and columns , If you want to access the cells in the rows and columns , You can go further

from docx import Document
doc = Document()
table = doc.add_table(5, 3, )
print(table.rows) # Get all rows 
print(table.columns) # Get all columns 
# Traverse cells by row 
for row in table.rows:
for cell in row.cells:
print(cell)
# Traverse cells by column 
for col in table.columns:
for cell in col.cells:
print(cell)

_Rows and _Columns One of the objects is table Property can return the table object to which it belongs

doc = Document()
table = doc.add_table(5, 3, )
for row in table.rows:
print(row.table)
for col in table.columns:
print(col.table)

Through the above traversal, we can find that _Rows and _Columns Is to include Row and Column The iteratable object of , You can get them separately through traversal Row and Column object , and Row and Column The object is also very simple , The properties of both are the same , as follows

for row in table.rows:
print(row.cells) # All cells 
print(row.height) # Height 
# The first 2 Change the row height to 30 pounds 
if row._index == 2:
row.height = Pt(30)
print(row.height_rule) # Rules for specifying height 
print(row.table) # Current table object 
print(row._index) # Subscript 

3. Cell object

3.1 obtain Cell object

python-docx of use Cell For cells , Get the cell object in addition to the above nested loop , You can also get by subscript

doc = Document()
table = doc.add_table(5, 3, )
# For the first 1 Xing di 3 Cell of column ( Subscript from 0 Start )
cell1 = table.cell(0, 2)
3.2 Modify cell text

If you want to change the text of a cell , Can be modified directly Cell Object's text attribute , In fact, it is also a paragraph that gets a cell and then modifies it , So there are two ways

from docx import Document
doc = Document()
table = doc.add_table(5, 3, )
# For the first 1 Xing di 3 Cell of column ( Subscript from 0 Start )
cell1 = table.cell(0, 2)
cell1.text = " Cold hope "
cell2 = table.cell(1, 2)
paragraph= cell2.paragraphs[0]
run = paragraph.add_run(" Bingbing is very handsome ")
3.3 merge cell
...
cell3 = table.cell(2, 1)
cell4 = table.cell(3, 2)
cell3.merge(cell4)
3.4 Cell style

You can set the style of the entire table , You can also style the cells individually , Show cell styles first

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm, RGBColor, Pt
...
cell2 = table.cell(1, 2)
paragraph = cell2.paragraphs[0]
run = paragraph.add_run(" Bingbing is very handsome ")
# Set cell style 
run.font.color.rgb = RGBColor.from_string("00FFFF") # The font color 
run.font.size = Cm(1) # font size ,1 centimeter 
paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # Align center 
# Set table style , But the cell style takes precedence 
table.style.font.size = Pt(15) # font size 15 pounds 
table.style.font.color.rgb = RGBColor.from_string("6495ED") # The font color 
table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT # Align left 
3.5 Wide height
...
# The maximum value in the same row or column shall prevail 
table.cell(0, 0).width = Cm(3)
table.rows[0].height = Cm(2)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved