Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write doc,txt,xls documents by python

Shulou Source: shulou.com Published: 2022-06-01 01:45:31 09月21日 Update

This article introduces the relevant knowledge of "how to read and write doc,txt,xls documents by python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Reading doc documents by 1.python

# example of reading text code in docx

Import docx

# get document object

File=docx.Document ("path")

Print ("number of paragraphs:" + str (len (file.paragraphs) # the number of paragraphs is 13, and each carriage return isolates one paragraph

# output the contents of each paragraph

For para in file.paragraphs:

Print (para.text)

# output paragraph number and paragraph content

For i in range (len (file.paragraphs)):

The content of print (paragraph "+ str (I) +") is: "+ file.graphies [I] .text)

Reading txt documents by 2.python

Filename = 'tangqing.txt' # txt file is in the same directory as the current script, so you don't have to write a specific path

Pos = []

Efield = []

With open (filename,'r') as file_to_read:

While True:

Lines = file_to_read.readline () # whole row read data

If not lines:

Break

Pairtmp = [float (I) for i in lines.split ()] # splits the entire line of data. If the separator is a space, there is no parameter in parentheses, and if it is a comma, pass in the', 'character.

Pos = np.array (p_tmp) # converts data from list type to array type.

Print (pos)

Reading xls Table by 3.python

Import xdrlib, sys

Import xlrd

Def open_excel (file= 'path'):

Try:

Data = xlrd.open_workbook (file)

Return data

Except Exception as e:

Print (str (e))

# get the data parameters in the Excel table according to the index: file:Excel file path colnameindex: the index of the row where the header column name is located, and by_index: the index of the table

Def excel_table_byindex (file= 'path/xxx.xls',colnameindex=0,by_index=0):

Data = open_excel (file)

Table = data.sheets () [by_index]

Nrows = table.nrows # rows

Ncols = number of table.ncols # columns

Colnames = table.row_values (colnameindex) # A row of data

List = []

For rownum in range (1):

Row = table.row_values (rownum)

If row:

App = {}

For i in range (len (colnames)):

App [colnamesI] = row [I]

List.append (app)

Return list

# get the data parameters in the Excel table according to the name: file:Excel file path colnameindex: the row where the header column name is located, so, by_name:Sheet1 name

Def excel_table_byname (file= 'e:\\ personal file\\ 6-desktop\\ Fengsha point meter-power distribution substation .xls', colnameindex=0,by_name=u' electricity'):

Data = open_excel (file)

Table = data.sheet_by_name (by_name)

Nrows = table.nrows # rows

Colnames = table.row_values (colnameindex) # A row of data

List = []

For rownum in range (1):

Row = table.row_values (rownum)

If row:

App = {}

For i in range (len (colnames)):

App [colnamesI] = row [I]

List.append (app)

Return list

Def main ():

Tables = excel_table_byindex ()

For row in tables:

Print (row)

Tables = excel_table_byname ()

For row in tables:

Print (row)

If _ _ name__== "_ _ main__":

Main ()

This is the end of the content of "how to read and write doc,txt,xls documents by python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data document content file paragraph parameter index table path output one line no name location more knowledge type header practical learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Huawei vpn Shulou Tech Info