How to read table contents in word by python
This article is about how python reads the contents of a table in word. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Word files look complex and not easy to structure. In fact, there are probably several kinds of content in word documents: paragraph (paragraphs), table (tables), and character (characters). The word documents I'm going to analyze now are basically paragraphs and tables. This paper mainly describes the method of analyzing the table from word and structuring the table information.
1. In order to parse word files using python, you can use the package docx, which needs to be installed in python first.
Pip install python-docx
2. After installation, you can read the word file.
Importdocxfn= rudder:\ everlasting song. Docx'doc=docx.Document (fn) # read all data by paragraph for paragraph indoc.paragraphs:print (paragraph.text) # read all data by table for table indoc.tables:for row intable.rows:for cell inrow.cells:print (cell.text) table_num=len (doc.tables) # get the number of tables print (table_num) table_0=doc.tables [0] # Select the first table table_rows = len (table_ 0.rows) # get the number of rows of the first table print (table_rows) tab=doc.tables [0] .rows [0] .cells [0] # get the first row of the first table, first column data print (tab.text) par= doc.paragraphs [2] # read the third paragraph of data print (par.text) Thank you for reading! This is the end of this article on "how python reads table contents in word". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!