Python Forum
docx file to pandas dataframe/excel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
docx file to pandas dataframe/excel
#2
Got an example docx file to work on?

If the files are unreadable, nothing will help!

This gets the tables to pandas:

from docx import Document
import pandas as pd

mydocxfile = '/home/pedro/myPython/docxFiles/example_table2.docx'
for table in Document(mydocxfile).tables:
    data = [[cell.text for cell in row.cells] for row in table.rows]
    print(pd.DataFrame(data[1:], columns=data[0]), "\n")
Gives:

Output:
Name Age Occupation 0 King Charles 75 King 1 Pedro 55 Layabout 2 Baby 32 Import-Export
Reply


Messages In This Thread
docx file to pandas dataframe/excel - by iitip92 - Jun-26-2024, 07:40 PM
RE: docx file to pandas dataframe/excel - by Pedroski55 - Jun-27-2024, 05:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Most efficient way to roll through a pandas dataframe? sawtooth500 1 182 Jun-07-2024, 03:37 AM
Last Post: sawtooth500
  Python openyxl not updating Excel file MrBean12 1 549 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 631 Feb-07-2024, 12:24 PM
Last Post: Viento
  no module named 'docx' when importing docx MaartenRo 1 1,368 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 866 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Search Excel File with a list of values huzzug 4 1,461 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 1,060 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 2,312 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Replace a text/word in docx file using Python Devan 4 4,621 Oct-17-2023, 06:03 PM
Last Post: Devan
  Question on pandas.dataframe merging two colums shomikc 4 996 Jun-29-2023, 11:30 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020