Read a Word document and extract and write data (based on Python 3.6)

Source: Internet
Author: User

#!/usr/bin/python3
#-*-Coding:utf-8-*-
# @File: Delete_file
# @Author: Moucong
# @Date: 2018/4/1 16:33
# @Software: Pycharm

#读取docx中的文本代码示例
Import docx
Import re

#获取文档
File=docx. Document ("E:\\python_word\\word.docx")
Print ("Number of paragraphs:" +str (Len (file.paragraphs))) #输出段落数
File_word = docx. Document ()

#输出每一段的内容
For para in file.paragraphs:
Print (Para.text)

#输出段落编号及段落内容
Para_data = []
For I in range (len (file.paragraphs)):
# for J in Map (Lambda x:x.split ("), File.paragraphs[i].text):
Para_single = File.paragraphs[i].text.split (")
While "in Para_single: # Remove spaces
Para_single.remove (")
# para_data.append (Para_single)
For Data_number in range (len (para_single)):
Data_num = Re.findall (r "\d", Para_single[data_number])
Data_num = ". Join (Data_num)
Para_data.append (Data_num + ")
File_word.add_paragraph (Para_data)
File_word.save ("E:\\python_word\\number.docx")

Read a Word document and extract and write data (based on Python 3.6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.