2024-10-10 21:03:02 +08:00

42 lines
1.3 KiB
Python

from docx import Document
def read_docx(file_path):
# 尝试打开文档
try:
doc = Document(file_path)
except Exception as e:
print(f"Error opening file: {e}")
return
# 读取文档中的所有段落并打印它们
for para in doc.paragraphs:
print(para.text)
def read_docx_tables(file_path):
# 尝试打开文档
try:
doc = Document(file_path)
except Exception as e:
print(f"Error opening file: {e}")
return
# 读取文档中的所有表格
if not doc.tables:
print("No tables found in the document.")
return
# 遍历文档中的每个表格
for table_idx, table in enumerate(doc.tables):
print(f"Table {table_idx + 1}:")
# 遍历表格中的每一行
for row_idx, row in enumerate(table.rows):
row_data = []
# 遍历每一行中的单元格
for cell in row.cells:
row_data.append(cell.text.strip()) # 去除单元格内容前后空白
print(f"Row {row_idx + 1}: {row_data}")
print("\n" + "-" * 40 + "\n") # 打印分隔线
if __name__ == "__main__":
file_path="C:\\Users\\Administrator\\Desktop\\货物标\\output4\\磋商文件(1)_tobidders_notice_part1.docx"
# read_docx(file_path)
read_docx_tables(file_path)