1. 程式人生 > >最全總結 | 聊聊 Python 辦公自動化之 Word(中)

最全總結 | 聊聊 Python 辦公自動化之 Word(中)

![image](https://upload-images.jianshu.io/upload_images/1466987-09f606f916f73b1c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## 1\. 前言 上一篇文章,對 Word 寫入資料的一些常見操作進行了總結 [最全總結 | 聊聊 Python 辦公自動化之 Word(上)](http://mp.weixin.qq.com/s?__biz=MzU1OTI0NjI1NQ==&mid=2247486899&idx=1&sn=d25d979b7cab442a5a062ded7f4cf31a&chksm=fc1b7373cb6cfa6544c802baab9c46aeeb5d0d26830c61ca9c3843bdc57927d0df5da868ae55&scene=21#wechat_redirect) 相比寫入資料,讀取資料同樣很實用! 本篇文章,將談談如何全面讀取一個 Word 文件中的資料,並會指出一些要注意的點 ## 2\. 基本資訊 我們同樣使用 python-docx 這個依賴庫來對 Word 文件進行讀取 首先我們來讀取文件的基本資訊 它們分別是:章節、頁邊距、頁首頁尾邊距、頁面寬高、頁面方向等 在獲取文件基礎資訊之前,我們通過文件路徑構建一個文件物件 Document ``` from docx import Document # 原始檔目錄 self.word_path = './output.docx' # 開啟文件,構建一個文件物件 self.doc = Document(self.word_path) ``` 1 - 章節( Section ) ``` # 1、獲取章節資訊 # 注意:章節可以設定本頁的大小、頁首、頁尾 msg_sections = self.doc.sections print("章節列表:", msg_sections) # 章節數目 print('章節數目:', len(msg_sections)) ``` 2 - 頁邊距( Page Margin ) 通過章節物件的 left_margin、top_margin、right_margin、bottom_margin 屬性值可以獲取當前章節的左邊距、上邊距、右邊距、下邊距 ``` def get_page_margin(section): """ 獲取某個頁面的頁邊距(EMU) :param section: :return: """ # 分別對應:左邊距、上邊距、右邊距、下邊距 left, top, right, bottom = section.left_margin, section.top_margin, section.right_margin, section.bottom_margin return left, top, right, bottom # 2、頁邊距資訊 first_section = msg_sections[0] left, top, right, bottom = get_page_margin(first_section) print('左邊距:', left, ",上邊距:", top, ",右邊距:", right, ",下邊距:", bottom) ``` 返回值的單位是 EMU,和釐米、英尺的轉換關係如下: ![image](https://upload-images.jianshu.io/upload_images/1466987-27435072d6ef31b3?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 3 - 頁首頁尾邊距 頁首邊距:header_distance 頁尾邊距:footer_distance ``` def get_header_footer_distance(section): """ 獲取頁首、頁尾邊距 :param section: :return: """ # 分別對應頁首邊距、頁尾邊距 header_distance, footer_distance = section.header_distance, section.footer_distance return header_distance, footer_distance # 3、頁首頁尾邊距 header_distance, footer_distance = get_header_footer_distance(first_section) print('頁首邊距:', header_distance, ",頁尾邊距:", footer_distance) ``` 4 - 頁面寬度和高度 頁面寬度:page_width 頁面高度:page_height ``` def get_page_size(section): """ 獲取頁面寬度、高度 :param section: :return: """ # 分別對應頁面寬度、高度 page_width, page_height = section.page_width, section.page_height return page_width, page_height # 4、頁面寬度、高度 page_width, page_height = get_page_size(first_section) print('頁面寬度:', page_width, ",頁面高度:", page_height) ``` 5 - 頁面方向( Page Orientation ) 頁面方向分為:橫向和縱向 使用章節物件的 orientation 屬性去獲取一個章節的頁面方向 ``` def get_page_orientation(section): """ 獲取頁面方向 :param section: :return: """ return section.orientation # 5、頁面方向 # 型別:class 'docx.enum.base.EnumValue # 包含:PORTRAIT (0)、LANDSCAPE (1) page_orientation = get_page_orientation(first_section) print("頁面方向:", page_orientation) ``` 同樣,可以直接使用這個屬性設定一個章節的方向 ``` from docx.enum.section import WD_ORIENT # 設定頁面方向(橫向、豎向) # 設定為橫向 first_section.orientation = WD_ORIENT.LANDSCAPE # 設定為豎向 # first_section.orientation = WD_ORIENT.PORTRAIT self.doc.save(self.word_path) ``` ## 3\. 段落 使用文件物件的 paragraphs 屬性可以獲取文件中所有的段落 注意:這裡獲取的段落不包含頁首、頁尾、表格中的段落 ``` # 獲取文件物件中所有的段落,預設不包含:頁首、頁尾、表格中的段落 paragraphs = self.doc.paragraphs # 1、段落數目 paragraphs_length = len(paragraphs) print('文件中一共包含:{}個段落'.format(paragraphs_length)) ``` 1 - 段落內容 我們可以遍歷文件中所有的段落列表,通過段落物件的 text 屬性,獲取全部的段落內容 ``` # 0、讀取所有段落資料 contents = [paragraph.text for paragraph in self.doc.paragraphs] print(contents) ``` 2 - 段落格式 通過上一篇文章,我們知道段落也存在格式的 使用 paragraph_format 屬性獲取段落的基本格式資訊 包含:對齊方式、左右縮排、行間距、段落前後間距等 ``` # 2、獲取某一個段落的格式資訊 paragraph_someone = paragraphs[0] # 2.1 段落內容 content = paragraph_someone.text ​print('段落內容:', content) # 2.2 段落格式 paragraph_format = paragraph_someone.paragraph_format # 2.2.1 對齊