Python實(shí)現(xiàn)快速提取Word表格并寫入Excel
在日常辦公中,常常會(huì)遇到需要從Word文檔中提取表格內(nèi)容,并將其寫入Excel表格的需求。通過使用Python編程語(yǔ)言,我們可以高效地完成這一任務(wù)。本文將詳細(xì)介紹如何使用Python提取Word文檔表格內(nèi)容并寫入Excel,提供完整的代碼示例。
一、環(huán)境準(zhǔn)備
在開始編寫代碼之前,我們需要安裝一些Python庫(kù)來處理Word和Excel文檔。主要使用到的庫(kù)有python-docx和openpyxl。
1. 安裝python-docx庫(kù)
python-docx庫(kù)用于讀取和操作Word文檔。使用以下命令安裝:
pip install python-docx
2. 安裝openpyxl庫(kù)
openpyxl庫(kù)用于讀取和寫入Excel文件。使用以下命令安裝:
pip install openpyxl
二、讀取Word文檔中的表格
首先,需要編寫代碼來讀取Word文檔中的表格內(nèi)容。以下是一個(gè)示例代碼,用于從Word文檔中提取所有表格內(nèi)容并打印出來。
示例代碼:
from docx import Document def read_word_tables(file_path): doc = Document(file_path) tables = doc.tables data = [] for table in tables: table_data = [] for row in table.rows: row_data = [] for cell in row.cells: row_data.append(cell.text) table_data.append(row_data) data.append(table_data) return data ???????# 示例用法 word_file = 'example.docx' tables = read_word_tables(word_file) for i, table in enumerate(tables): print(f"Table {i+1}:") for row in table: print("\t".join(row))
在這個(gè)示例中,read_word_tables函數(shù)接受一個(gè)Word文件的路徑,返回一個(gè)包含所有表格內(nèi)容的列表。每個(gè)表格內(nèi)容以二維列表的形式存儲(chǔ),其中每個(gè)子列表代表一行,每個(gè)子列表中的元素代表一個(gè)單元格的內(nèi)容。
三、將表格內(nèi)容寫入Excel
將提取的表格內(nèi)容寫入Excel文件。以下是一個(gè)示例代碼,用于將表格內(nèi)容寫入Excel文件。
示例代碼:
from openpyxl import Workbook def write_to_excel(file_path, tables): wb = Workbook() ws = wb.active for table in tables: for row in table: ws.append(row) ws.append([]) # 添加一個(gè)空行,分隔不同的表格 wb.save(file_path) # 示例用法 excel_file = 'output.xlsx' write_to_excel(excel_file, tables)
在這個(gè)示例中,write_to_excel函數(shù)接受一個(gè)Excel文件的路徑和表格內(nèi)容列表,將表格內(nèi)容寫入Excel文件。使用openpyxl庫(kù)的Workbook對(duì)象創(chuàng)建一個(gè)新的工作簿,并通過ws.append方法將每行數(shù)據(jù)添加到工作表中。
四、完整示例:從Word提取表格并寫入Excel
將上述步驟結(jié)合起來,編寫一個(gè)完整的示例代碼,從Word文檔中提取表格內(nèi)容并寫入Excel文件。
示例代碼:
from docx import Document from openpyxl import Workbook def read_word_tables(file_path): doc = Document(file_path) tables = doc.tables data = [] for table in tables: table_data = [] for row in table.rows: row_data = [] for cell in row.cells: row_data.append(cell.text) table_data.append(row_data) data.append(table_data) return data def write_to_excel(file_path, tables): wb = Workbook() ws = wb.active for table in tables: for row in table: ws.append(row) ws.append([]) # 添加一個(gè)空行,分隔不同的表格 wb.save(file_path) # 示例用法 word_file = 'example.docx' excel_file = 'output.xlsx' tables = read_word_tables(word_file) write_to_excel(excel_file, tables) print(f"已成功將Word文檔中的表格內(nèi)容提取并寫入Excel文件:{excel_file}")
實(shí)際應(yīng)用中的考慮事項(xiàng)
在實(shí)際應(yīng)用中,處理Word文檔和Excel文件時(shí)可能會(huì)遇到一些特殊情況和問題。
1. 處理復(fù)雜表格
Word文檔中的表格可能具有復(fù)雜的結(jié)構(gòu),例如合并單元格、嵌套表格等。處理這些復(fù)雜表格時(shí),需要額外的代碼邏輯來處理這些特殊情況。
2. 表格數(shù)據(jù)清洗
從Word文檔提取的表格數(shù)據(jù)可能包含一些多余的空格或換行符。在寫入Excel之前,可以對(duì)數(shù)據(jù)進(jìn)行清洗,以確保數(shù)據(jù)的整潔和一致性。
3. 大文件處理
對(duì)于包含大量表格的大型Word文檔或需要寫入大量數(shù)據(jù)的Excel文件,可能需要考慮內(nèi)存和性能問題。可以采用分批讀取和寫入的方式來處理大文件。
示例代碼:
import re from docx import Document from openpyxl import Workbook def clean_text(text): # 去除多余的空格和換行符 return re.sub(r'\s+', ' ', text).strip() def read_word_tables(file_path): doc = Document(file_path) tables = doc.tables data = [] for table in tables: table_data = [] for row in table.rows: row_data = [] for cell in row.cells: row_data.append(clean_text(cell.text)) table_data.append(row_data) data.append(table_data) return data def write_to_excel(file_path, tables): wb = Workbook() ws = wb.active for table in tables: for row in table: ws.append(row) ws.append([]) # 添加一個(gè)空行,分隔不同的表格 wb.save(file_path) # 示例用法 word_file = 'example.docx' excel_file = 'output.xlsx' tables = read_word_tables(word_file) write_to_excel(excel_file, tables) print(f"已成功將Word文檔中的表格內(nèi)容提取并寫入Excel文件:{excel_file}")
總結(jié)
本文詳細(xì)介紹了如何使用Python從Word文檔中提取表格內(nèi)容并寫入Excel文件。通過使用python-docx庫(kù)讀取Word文檔,openpyxl庫(kù)寫入Excel文件,我們可以高效地完成這一任務(wù)。此外,本文還介紹了實(shí)際應(yīng)用中的一些考慮事項(xiàng)和解決方案。
以上就是Python實(shí)現(xiàn)快速提取Word表格并寫入Excel的詳細(xì)內(nèi)容,更多關(guān)于Python快速提取Word的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python繪制發(fā)散型柱狀圖+誤差陰影時(shí)間序列圖+雙坐標(biāo)系時(shí)間序列圖+繪制金字塔圖
這篇文章主要介紹了python繪制發(fā)散型柱狀圖+誤差陰影時(shí)間序列圖+雙坐標(biāo)系時(shí)間序列圖+繪制金字塔圖,詳細(xì)的內(nèi)容需要的小伙伴可以參考一下下面文章內(nèi)容2022-08-08Selenium基于PIL實(shí)現(xiàn)拼接滾動(dòng)截圖
這篇文章主要介紹了Selenium基于PIL實(shí)現(xiàn)拼接滾動(dòng)截圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04python 虛擬環(huán)境調(diào)用allure報(bào)錯(cuò):FileNotFoundError: [WinError
python代碼調(diào)用命令行 allure命令報(bào)錯(cuò),提示找不到allure這個(gè)命令,本文就詳細(xì)的介紹了具體的解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09淺談Pandas Series 和 Numpy array中的相同點(diǎn)
今天小編就為大家分享一篇淺談Pandas Series 和 Numpy array中的相同點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python實(shí)現(xiàn)楊輝三角的3種方法(迭代、生成器和遞歸)
這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)楊輝三角的3種方法,分別是迭代、生成器和遞歸的相關(guān)資料,楊輝三角形的規(guī)則就是每行的第一個(gè)數(shù)字和最后一個(gè)數(shù)字為1之外,其余每個(gè)數(shù)字等于上一行對(duì)應(yīng)兩個(gè)數(shù)字的和,需要的朋友可以參考下2023-11-11pyinstaller生成的exe文件啟動(dòng)時(shí)間漫長(zhǎng)的原因
本文主要介紹了pyinstaller生成的exe文件啟動(dòng)時(shí)間漫長(zhǎng)的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01