In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces Python how to embed the Word table into Excel, the content is very detailed, interested friends can refer to, hope to be helpful to you.
In fact, it is to transfer the table in Word to Excel and make an adjustment by the way. This demand is often encountered by many people in practical work.
If there are only two forms, you can simply copy and paste them, but what if there are hundreds? Then you have to consider automation. Fortunately, the original file format in the requirements encountered today is more regular, so give it a try.
# first pip install python-docx# if the original file is in doc format, then convert it to docxfrom docx import Documentimport pandas as pd path = ". / word table to excel.docx" docx = Document (path) table_s = docx.tables # return a list of Table objects len (table_s)-> 2 # the return value is 2, because there are only two tables in the original file list_ = [] # initialize an empty list Used to load the following dict_ for table in table_s: # loop all table lists dict_ = {} dict_ ['name'] = table.cell (0,1). The index of the text # table is from (0 row) Column 0) start dict_ ['identity'] = table.cell (0,3). Text dict_ ['introduction'] = table.cell (1,1). Text for i in range (3, len (table.rows)): # subsequent content format is not fixed, so loop get dict_ [table.cell (I, 0) .text] = table.cell (I, 1). Text list_.append (dict_)
When this is done, list_ is a list of dictionaries.
Next, change the list to DataFrame for operation.
Df = pd.DataFrame (list_) # first use the first three columns as the index, and then reverse the perspective of other columns In fact, this is an one-dimensional table to two-dimensional table process df = df.set_index (['name', 'identity', 'introduction']) .stack (level=0). To_frame () # reset the name of the row and column index df.index.names = ['name', 'identity', 'introduction', 'substance'] df.columns = ['status']
At this point, the df is not much different from the demand result.
Finally, the DataFrame is exported and the requirements can be completed with a little beautification.
# be sure to set index=True when exporting, or only one column of Seriesdf.to_excel ("data3.xlsx", index=True, merge_cells=True) will be retained.
Complete code
# first, pip install python-docx#. If the original file is in doc format, convert it to docxfrom docx import Documentimport pandas as pd path = ". / word table to excel.docx" docx = Document (path) table_s = docx.tables # return a list of Table objects list_ = [] # initialize an empty list Used to load the following dict_ for table in table_s: # loop all table lists dict_ = {} dict_ ['name'] = table.cell (0,1). The index of the text # table is from (0 row) Column 0) start with dict_ ['identity'] = table.cell (0,3). Text dict_ ['introduction'] = table.cell (1,1). Text for i in range (3, len (table.rows)): # loop to get the following content dict_ [table.cell (I, 0) .text] = table.cell (I 1). Text list_.append (dict_) df = pd.DataFrame (list_) # use the first three columns as the index Then invert perspective of other columns It's a process of converting an one-dimensional table to a two-dimensional table df = df.set_index (['name', 'identity', 'introduction']) .stack (level=0). To_frame () # reset the name of the row and column index df.index.names = [name, identity, introduction' 'matter'] df.columns = ['status quo'] # be sure to set index=True when exporting, or it will only retain a column of Seriesdf.to_excel ("data3.xlsx", index=True, merge_cells=True) about how Python implements embedding Word tables into Excel. That's it. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.