安装
- pip install python-docx
-
步骤1:打开Word文件
首先,我们需要打开要读取的Word文件。我们可以使用python-docx库来实现这一功能。
- # 导入所需的库
- from docx import Document
-
- # 打开Word文件
- doc = Document('file.docx')
-
步骤2:读取表格数据
接下来,我们需要读取Word表格中的数据。我们可以使用python-docx库的Table对象和Cell对象来实现这一功能。
- # 获取所有表格
- tables = doc.tables
-
- # 遍历表格
- for table in tables:
- # 获取表格中的所有行
- rows = table.rows
- for row in rows:
- # 获取行中的所有单元格
- cells = row.cells
- for cell in cells:
- # 获取单元格的文本内容
- text = cell.text
-
步骤3:保留原格式
在读取表格数据时,我们需要保留原始的格式,包括字体、颜色、对齐方式等。我们可以使用python-docx库的Run对象和Paragraph对象来实现这一功能。
- # 获取所有段落
- paragraphs = doc.paragraphs
-
- # 遍历段落
- for paragraph in paragraphs:
- # 获取段落中的所有Run对象
- runs = paragraph.runs
- for run in runs:
- # 获取字体、颜色等属性
- font = run.font
- color = font.color
- # 保留格式
-
步骤4:输出数据
最后,我们需要将读取到的表格数据输出到文件或者控制台。在这里,我们将数据输出到控制台。
- # 输出数据
- print(text)
-
完整代码示例
下面是一个完整的示例代码,演示了如何使用Python读取Word表格数据并保留原格式。
- from docx import Document
-
- # 打开Word文件
- doc = Document('file.docx')
-
- # 获取所有表格
- tables = doc.tables
-
- # 遍历表格
- for table in tables:
- # 获取表格中的所有行
- rows = table.rows
- for row in rows:
- # 获取行中的所有单元格
- cells = row.cells
- for cell in cells:
- # 获取单元格的文本内容
- text = cell.text
- # 输出数据
- print(text)
-
- # 获取所有段落
- paragraphs = doc.paragraphs
-
- # 遍历段落
- for paragraph in paragraphs:
- # 获取段落中的所有Run对象
- runs = paragraph.runs
- for run in runs:
- # 获取字体、颜色等属性
- font = run.font
- color = font.color
- # 保留格式
-
- # 输出数据
- print(paragraph.text)
-
- from docx import Document
- from docx.enum.text import WD_PARAGRAPH_ALIGNMENT #设置对象居中、对齐等。
- from docx.enum.text import WD_TAB_ALIGNMENT,WD_TAB_LEADER #设置制表符等
- from docx.shared import Inches #设置图像大小
- from docx.shared import Pt #设置像素、缩进等
- from docx.shared import RGBColor #设置字体颜色
- from docx.shared import Length #设置宽度
- from docx.oxml.ns import qn
- import time
- today=time.strftime("%Y{y}%m{m}%d{d}",time.localtime()).format(y="年",m="月",d="日")
-
- document=Document()
- document.styles["Normal"].font.name=u'宋体'# 设置文档的基础字体
- document.styles["Normal"].element.rPr.rFonts.set(qn('w:eastAsia'),u'宋体')#设置文档的基础中文字体
-
-
- #初始化建立第一个自然段
- p1=document.add_paragraph()
- p1.alignment=WD_PARAGRAPH_ALIGNMENT.CENTER#对齐方式为居中
- run1=p1.add_run("关于下达%s产品的通知"%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磅
-
- # 建立第一个自然段
- 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
-
- # 建立第一个自然段
- p3 = document.add_paragraph()
- run3 = p3.add_run(" 根据公司安排,为提供优质客户服务,我单位将价格通知如下:" )
- 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产品报价表')# 合并单位格内填入XX产品报价表
- 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 = '价格'
- 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()#分页符
- document.save('价格通知.docx')#保存
-
需要说明的是
- run3.font.name = “仿宋_GB2312”
- run3.element.rPr.rFonts.set(qn(‘w:eastAsia’), u’仿宋_GB2312′)
-
这两句均是设置字体为仿宋_GB2312,之所以要两种格式写两遍,是因为word对中文支持不太友好,需要再填一句
安装
- pip install docx2txt
-
代码
- import docx2txt
-
- text = docx2txt.process("example.docx")
- print(text)
-