python代码实现读取excel数据 输出到word
时间: 2023-05-04 22:04:34 浏览: 119
Python 可以通过第三方库 openpyxl 和 python-docx 来实现读取 Excel 数据并输出到 Word 文档。
首先,需要安装这两个库:
```
pip install openpyxl
pip install python-docx
```
然后,导入库:
```python
import openpyxl
from docx import Document
from docx.shared import Cm
```
接着,打开 Excel 文件,并读取数据:
```python
workbook = openpyxl.load_workbook(filename='example.xlsx')
sheet = workbook.active
data = []
for row in sheet.iter_rows(min_row=2, values_only=True):
data.append(row)
```
其中,min_row 参数指定从哪一行开始读取数据,values_only 参数指定只读取单元格的值。
然后,创建一个空的 Word 文档,设置页边距,并将数据写入文档:
```python
doc = Document()
# 设置页边距
sections = doc.sections
for section in sections:
section.top_margin = Cm(1)
section.bottom_margin = Cm(1)
section.left_margin = Cm(1)
section.right_margin = Cm(1)
# 将数据写入文档
table = doc.add_table(rows=1, cols=len(sheet.columns))
# 添加表头
hdr_cells = table.rows[0].cells
for i, cell in enumerate(hdr_cells):
cell.text = sheet.cell(row=1, column=i+1).value.strip()
# 添加数据
for row_data in data:
row_cells = table.add_row().cells
for i, cell in enumerate(row_cells):
cell.text = str(row_data[i]).strip()
doc.save('example.docx')
```
其中,sections 对象表示 Word 文档中的章节,我们可以通过它来设置页面布局,例如设置页边距。table 对象表示 Word 文档中的表格,我们可以通过它来添加表头和数据。对于数据,我们需要将它们转换成字符串,并去除多余的空格。
最后,保存文档即可。
完整代码如下:
```python
import openpyxl
from docx import Document
from docx.shared import Cm
workbook = openpyxl.load_workbook(filename='example.xlsx')
sheet = workbook.active
data = []
for row in sheet.iter_rows(min_row=2, values_only=True):
data.append(row)
doc = Document()
# 设置页边距
sections = doc.sections
for section in sections:
section.top_margin = Cm(1)
section.bottom_margin = Cm(1)
section.left_margin = Cm(1)
section.right_margin = Cm(1)
# 将数据写入文档
table = doc.add_table(rows=1, cols=len(sheet.columns))
# 添加表头
hdr_cells = table.rows[0].cells
for i, cell in enumerate(hdr_cells):
cell.text = sheet.cell(row=1, column=i+1).value.strip()
# 添加数据
for row_data in data:
row_cells = table.add_row().cells
for i, cell in enumerate(row_cells):
cell.text = str(row_data[i]).strip()
doc.save('example.docx')
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)