Python操作word和excel

 时间:2026-02-12 22:48:46

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

  • ACCESS 2010如何给数据库加密
  • 在VBA中,如何编写添加批注代码?
  • 怎样使用VBA提升第三个条件优先级为1?
  • 如何实现在access窗体上打印窗体当前记录的报表
  • excel中使用vba来获取系统信息的方法
  • 热门搜索
    金庸群侠传2攻略 大连美食攻略 红烧牛肉的做法 最正宗的做法 红楼炮灰攻略 大图书馆的牧羊人攻略 会声会影怎么用 基础代谢率减肥法 杭州攻略 工装裤搭配 减肥期间早餐吃什么好