Python word實(shí)現(xiàn)讀取及導(dǎo)出代碼解析
2個(gè)簡單的代碼,幫你實(shí)現(xiàn)word的導(dǎo)出和word的讀取
功能一:導(dǎo)出word,word中的內(nèi)容為
代碼:
from docx import Documentfrom 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 qnimport timetoday=time.strftime('%Y{y}%m{m}%d0gym22g6m8ya',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'隸書'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 = ’價(jià)格’table.cell(1, 2).text = ’備注’table.cell(2, 0).text = todaytable.cell(2, 1).text =’100’table.cell(2, 2).text = ’’document.add_page_break()#分頁符document.save(’價(jià)格通知.docx’)#保存
需要說明的是
run3.font.name = '仿宋_GB2312'run3.element.rPr.rFonts.set(qn(’w:eastAsia’), u’仿宋_GB2312’)
這兩句均是設(shè)置字體為仿宋_GB2312,之所以要兩種格式寫兩遍,是因?yàn)閣ord對(duì)中文支持不太友好,需要再填一句
功能二:讀取word,word中的內(nèi)容為
讀取表格外文字的代碼:
from docx import Documentdocument=Document('長恨歌.docx')print('讀取非表格中的內(nèi)容:')all_paragraphs=document.paragraphsfor paragraph in all_paragraphs: print(paragraph.text)
讀取表格內(nèi)文字的代碼:
from docx import Documentdocument=Document('長恨歌.docx')print('讀取表格中的內(nèi)容:')tables=document.tablesfor 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))
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項(xiàng)目的過程8. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
