Python操作word和excel

2025-10-03 11:26:29

1、载入模块xlrd,打开工作表open_workbook()

import xlrd

path='C:\\Users\\jyjh\\Desktop\\data.xlsx'

#open the workbook

data=xlrd.open_workbook(path)

#get the data fromthe sheet

#select the sheet byindex

sheet1=data.sheet_by_index(0)

#select the sheet bylist index

sheet1=data.sheets()[0]

#select the sheet byname

sheet1=data.sheet_by_name(u'Sheet1')

#get the sheet rowvalues

row1=sheet1.row_values(1)

#get the columnvalues

column1=sheet1.col_values(1)

#get the sheetnumber of rows

number_of_rows=sheet1.nrows

#get the number ofcolumns

number_of_columns=sheet1.ncols

#traverse the sheetby rows

for i inrange(number_of_rows):

    print(sheet1.row_values(i))

#get the cell value

cell_A1=sheet1.cell(0,0).value

cell_A2=sheet1.cell(1,0).value

cell_A1=sheet1.row(0)[0].value

cell_A2=sheet1.col(1)[0].value

#the simple writevalue

row=0

col=0

#typeempty:0;string:1;number:2;date:3;boolean:4;error:5

celltype=1

value='数值'

xf=0

sheet1.put_cell(row,col,celltype,value,xf)

sheet1.cell(0,0)

sheet1.cell(0,0).value

Python操作word和excel

Python操作word和excel

2、新建一个word文档或者打开一个已存在word文档

importwin32com.client

wordapp=win32com.client.Dispatch("Word.Application")

#create a new blankdocument

docx=wordapp.Documents.Add()

#open an exitdocument

docx=wordapp.Documents.Open(filepath)

Python操作word和excel

3、对新建的word文档进行操作,包括设置字体形式,

importwin32com.client

wordapp=win32com.client.Dispatch("Word.Application")

#create a new blankdocument

docx=wordapp.Documents.Add()

select=wordapp.Selection

#select the fontstyle

#fontname="黑体","宋体"

select.Font.Name="黑体"

#fontsize="大一号","五号"

select.Font.Size=24

#bold,italic,underline

select.Font.Underline=True

select.Font.Italic=True

#alignment 0:left1:center 2:right

select.ParagraphFormat.Alignment=0

#type something

select.TypeText("To:whom")

select.TypeText("\n")

select.Font.Name="宋体"

select.Font.Size=15

select.Font.Italic=True

select.Font.Underline=False

select.ParagraphFormat.Alignment=2

select.TypeText("From:whom")

select.TypeText("\n")

#create a table

table=docx.Tables.Add(select.Range,4,3)

table.Style="网格型"

table.Rows.Alignment=1

table.Cell(1,1).Range.Text="Table1:"

for i in range(2,5):

   table.Cell(i,1).Range.Text="row"+str(i)

for i in range(2,4):

   table.Cell(1,i).Range.Text="column"+str(i)

#docx.Save(path):savefile

docx.SaveAs('C:\\Users\\jyjh\\Desktop\\demo1.docx')

保存文档

Python操作word和excel

Python操作word和excel

4、通过两者的结合将Excel中的数据写入到Word中。

import xlrd

path='C:\\Users\\jyjh\\Desktop\\data.xlsx'

data=xlrd.open_workbook(path)

sheet1=data.sheet_by_index(0)

row1=sheet1.row_values(1)

import win32com.client

wordapp=win32com.client.Dispatch("Word.Application")

#create a new blank document

docx=wordapp.Documents.Add()

select=wordapp.Selection

select.Font.Name="黑体"

select.Font.Size=12

select.Font.Bold=True

select.TypeText(row1) 

docx.SaveAs('C:\\Users\\jyjh\\Desktop\\demo.docx')

Python操作word和excel

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢