从Word文件抽取数据导入到Excel文件
1
2
pip3 install python-docx;
pip3 install xlwl;

从docx抽取数据

1
2
3
4
5
6
7
8
9
import docx
def get_docx():
from docx import Document
path = "info.docx"
document = Document(path)
Lines = []
for paragraph in document.paragraphs:
Lines.append(paragraph.text)
return Lines

写入xls文件

1
2
3
4
5
6
import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet')
# 0行 0列 写入 something
worksheet.write(0, 0, label='something')
workbook.save('result.xls')

结合使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import docx, xlwt
from docx import Document

def get_docx():
path = "info.docx"
document = Document(path)
Lines = []
for paragraph in document.paragraphs:
Lines.append(paragraph.text)
return Lines

def main():
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet')

contents = get_docx()
for index, content in enumerate(contents):
print(content, index)
worksheet.write(index, 1, label=content)
workbook.save('result.xls')

if __name__ == '__main__':
main()
文章作者: Spaceack
文章链接: http://spaceack.com/2020/05/08/2020-05-08-%E4%BB%8EWord%E6%96%87%E4%BB%B6%E6%8A%BD%E5%8F%96%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%85%A5%E5%88%B0Excel%E6%96%87%E4%BB%B6/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 丸子家的小云吞
支付宝打赏
微信打赏