功能一:導出word,word中的內容為
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT #設置對象居中、對齊等。 from docx.enum.text import WD_TAB_ALIGNMENT,WD_TAB_LEADER #設置制表符等 from docx.shared import Inches #設置圖像大小 from docx.shared import Pt #設置像素、縮進等 from docx.shared import RGBColor #設置字體顏色 from docx.shared import Length #設置寬度 from docx.oxml.ns import qn import time today = time.strftime( "%Y{y}%m{m}%dg8kyieogoc6" ,time.localtime()). format (y = "年" ,m = "月" ,d = "日" ) document = Document() document.styles[ "Normal" ].font.name = u '宋體' # 設置文檔的基礎字體 document.styles[ "Normal" ].element.rPr.rFonts. set (qn( 'w:eastAsia' ),u '宋體' ) #設置文檔的基礎中文字體 #初始化建立第一個自然段 p1 = document.add_paragraph() p1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #對齊方式為居中 run1 = p1.add_run( "關于下達%s產品的通知" % today) run1.font.name = "微軟雅黑" run1.font.size = Pt( 21 ) # 字體大小為21磅 run1.font.bold = True #加粗 p1.space_after = Pt( 5 ) #段后距離5磅 p1.space_before = Pt( 5 ) # 段前距離5磅 # 建立第一個自然段 i = '客戶3' p2 = document.add_paragraph() run2 = p2.add_run( "%s:" % i) run2.font.name = "仿宋_GB2312" run2.element.rPr.rFonts. set (qn( 'w:eastAsia' ), u '仿宋_GB2312' ) run2.font.size = Pt( 16 ) run2.font.bold = True # 建立第一個自然段 p3 = document.add_paragraph() run3 = p3.add_run( " 根據公司安排,為提供優質客戶服務,我單位將價格通知如下:" ) run3.font.name = "仿宋_GB2312" run3.element.rPr.rFonts. set (qn( 'w:eastAsia' ), u '仿宋_GB2312' ) run3.font.size = Pt( 16 ) run3.font.bold = True # 建立表格 table = document.add_table(rows = 3 ,cols = 3 ,style = 'Table Grid' ) table.cell( 0 , 0 ).merge(table.cell( 0 , 2 )) # 合并第一行 table_run1 = table.cell( 0 , 0 ).paragraphs[ 0 ].add_run( 'XX產品報價表' ) # 合并單位格內填入XX產品報價表 table_run1.font.name = u "隸書" table_run1.element.rPr.rFonts. set (qn( 'w:eastAsia' ), u '隸書' ) table.cell( 0 , 0 ).paragraphs[ 0 ].alighment = WD_PARAGRAPH_ALIGNMENT.CENTER #居中 table.cell( 1 , 0 ).text = '日期' table.cell( 1 , 1 ).text = '價格' table.cell( 1 , 2 ).text = '備注' table.cell( 2 , 0 ).text = today table.cell( 2 , 1 ).text = '100' table.cell( 2 , 2 ).text = '' document.add_page_break() #分頁符 document.save( '價格通知.docx' ) #保存 |
需要說明的是
run3.font.name = "仿宋_GB2312"
run3.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
這兩句均是設置字體為仿宋_GB2312,之所以要兩種格式寫兩遍,是因為word對中文支持不太友好,需要再填一句
功能二:讀取word,word中的內容為
讀取表格外文字的代碼:
1
2
3
4
5
6
|
from docx import Document document = Document( "長恨歌.docx" ) print ( "讀取非表格中的內容:" ) all_paragraphs = document.paragraphs for paragraph in all_paragraphs: print (paragraph.text) |
讀取表格內文字的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from docx import Document document = Document( "長恨歌.docx" ) print ( "讀取表格中的內容:" ) tables = document.tables for i in range ( len (tables)): tb = tables[i] #獲取表格的行 tb_rows = tb.rows #讀取每一行內容 for i in range ( len (tb_rows)): row_data = [] row_cells = tb_rows[i].cells #讀取每一行單元格內容 for cell in row_cells: #單元格內容 row_data.append(cell.text) print (''.join(row_data)) |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/hzcjd/p/13263415.html