您好,登錄后才能下訂單哦!
小編給大家分享一下Python中word實(shí)現(xiàn)讀取及導(dǎo)出的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
2個(gè)簡(jiǎn)單的代碼,幫你實(shí)現(xiàn)word的導(dǎo)出和word的讀取
功能一:導(dǎo)出word,word中的內(nèi)容為
代碼:
from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT #設(shè)置對(duì)象居中、對(duì)齊等。 from docx.enum.text import WD_TAB_ALIGNMENT,WD_TAB_LEADER #設(shè)置制表符等 from docx.shared import Inches #設(shè)置圖像大小 from docx.shared import Pt #設(shè)置像素、縮進(jìn)等 from docx.shared import RGBColor #設(shè)置字體顏色 from docx.shared import Length #設(shè)置寬度 from docx.oxml.ns import qn import time today=time.strftime("%Y{y}%m{m}%dq0hc4fa",time.localtime()).format(y="年",m="月",d="日") document=Document() document.styles["Normal"].font.name=u'宋體'# 設(shè)置文檔的基礎(chǔ)字體 document.styles["Normal"].element.rPr.rFonts.set(qn('w:eastAsia'),u'宋體')#設(shè)置文檔的基礎(chǔ)中文字體 #初始化建立第一個(gè)自然段 p1=document.add_paragraph() p1.alignment=WD_PARAGRAPH_ALIGNMENT.CENTER#對(duì)齊方式為居中 run1=p1.add_run("關(guān)于下達(dá)%s產(chǎn)品的通知"%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磅 # 建立第一個(gè)自然段 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 # 建立第一個(gè)自然段 p3 = document.add_paragraph() run3 = p3.add_run(" 根據(jù)公司安排,為提供優(yōu)質(zhì)客戶服務(wù),我單位將價(jià)格通知如下:" ) 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產(chǎn)品報(bào)價(jià)表')# 合并單位格內(nèi)填入XX產(chǎn)品報(bào)價(jià)表 table_run1.font.name = u"隸書(shū)" table_run1.element.rPr.rFonts.set(qn('w:eastAsia'), u'隸書(shū)') table.cell(0, 0).paragraphs[0].alighment=WD_PARAGRAPH_ALIGNMENT.CENTER#居中 table.cell(1, 0).text='日期' table.cell(1, 1).text = '價(jià)格' 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()#分頁(yè)符 document.save('價(jià)格通知.docx')#保存
需要說(shuō)明的是
run3.font.name = "仿宋_GB2312"
run3.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
這兩句均是設(shè)置字體為仿宋_GB2312,之所以要兩種格式寫(xiě)兩遍,是因?yàn)閣ord對(duì)中文支持不太友好,需要再填一句
功能二:讀取word,word中的內(nèi)容為
讀取表格外文字的代碼:
from docx import Document document=Document("長(zhǎng)恨歌.docx") print("讀取非表格中的內(nèi)容:") all_paragraphs=document.paragraphs for paragraph in all_paragraphs: print(paragraph.text)
讀取表格內(nèi)文字的代碼:
from docx import Document document=Document("長(zhǎng)恨歌.docx") print("讀取表格中的內(nèi)容:") tables=document.tables for i in range(len(tables)): tb=tables[i]#獲取表格的行 tb_rows=tb.rows #讀取每一行內(nèi)容 for i in range(len(tb_rows)): row_data=[] row_cells=tb_rows[i].cells#讀取每一行單元格內(nèi)容 for cell in row_cells:#單元格內(nèi)容 row_data.append(cell.text) print(''.join(row_data))
看完了這篇文章,相信你對(duì)Python中word實(shí)現(xiàn)讀取及導(dǎo)出的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。