項目場景:
提示:這里簡述項目相關(guān)背景:
例如:客戶要求設(shè)置word 中所有表格的樣式,包括行間距、縮進、字體、字號、顏色、磅值等。
問題描述
提示:RPA沒有這么多的操作命令,只能靠python的win32com模塊或docx 模塊進行解決。
from win32com.client import Dispatch
def docx_table_style(filepath, font_style, font_size, bangs):
"""
修改word 表格字體樣式
:param filepath:文件地址
:param font_style:字體
:param font_size:字號
:param bangs:行間距磅數(shù)
:return:
"""
app = Dispatch('Word.Application')
# 讀取文件
doc = app.Documents.Open(filepath)
# 遍歷表格 及 行列
for table in doc.Tables:
row_cont = table.Rows.Count
col_cont = table.Columns.Count
# print(row_cont, col_cont)
for i in range(1, row_cont + 1):
for j in range(1, col_cont + 1):
try:
# 字體
table.Cell(i, j).Range.Font.NameFarEast = font_style
table.Cell(i, j).Range.Font.NameAscii = "Times New Roman"
table.Cell(i, j).Range.Font.NameOther = "Times New Roman"
# 顏色
# table.Cell(i, j).Range.Font.Color = 255
# 字號
table.Cell(i, j).Range.Font.Size = font_size
# 行間距
# table.Cell(i, j).Range.ParagraphFormat.LineSpacingRule = 1 # 單倍、1.5倍、雙倍行距分別為0, 1, 2
# 指定段落的左、右縮進 磅值。
# table.Cell(i, j).Range.ParagraphFormat.LeftIndent = bangs
# table.Cell(i, j).Range.ParagraphFormat.rightindent = bangs
# 行間距榜值
table.Cell(i, j).Range.ParagraphFormat.LineSpacing = bangs
except(Exception):
continue
doc.SaveAs(filepath)
doc.Close()
if __name__ == "__main__":
filepath1 = r"E:\desk\test\test.docx"
docx_table_style(filepath1, "宋體", 12, 15)
代碼簡介:
通過win32com模塊調(diào)用office接口,循環(huán)當(dāng)前word中的所有表格,然后遍歷表格的每行每列,依次設(shè)置word中表格的字體、字號等。注釋部分因為暫時不需要做設(shè)置, 就沒添加參數(shù)。需要的話可以自行解開注釋。文章來源:http://www.zghlxwxcb.cn/news/detail-526048.html
小結(jié):
這個函數(shù)代碼的注釋部分都已經(jīng)標(biāo)注詳細(xì),希望對大家有所幫助,另外,文章有錯誤的部分還請大家及時指正,共同探討,共同進步。文章來源地址http://www.zghlxwxcb.cn/news/detail-526048.html
到了這里,關(guān)于python 設(shè)置 word中所有表格樣式(行間距、縮進、字體、字號、顏色、磅值)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!