需要Python示例代码来打开、修改和保存现有的excel表

大家好,
我是新手,我已经写了非常基本和小的代码,我请求帮助以下人员.对于我的项目,我想写一个Python代码,它应该打开一个已经存在的Excel工作表,其中可以包含2或3个数据表和写数据在Sheet1(第一个工作表)单独,不应该修改其他数据表中的数据并保存它.
我听说人们说可以通过pyExcelerator、xlrd或xlw.因为所有这些东西对我来说都是新的..我真的不知道该怎么做.-(
因此,我请求你们所有人帮助我的样本代码,将尽可能早地完成上述任务.
谢谢,
阿木达

# 回答1


嗨!
我对Python并不陌生,所以下面是我用来做您所描述的事情的一个脚本示例:
(当然,您应该更改我使用的所有变量,并放置您自己的变量,
但由于我是在一些良好的研究(主要是在以下领域)提出的
VisualBasic...),我认为这对你来说是一个很好的起点.)
我希望这会有一点帮助...

选择 | 换行 | 行号
  1. from win32com.client import Dispatch
  2.  
  3. def Create_Analysis_Page(Name,Job,Phone,Month,Analysing_Dict,preview=False):
  4.  
  5.     xlApp = Dispatch ("Excel.Application")
  6.     xlWb = xlApp.Workbooks.Open ("C:\\Python25\\Misthos.xlsx")
  7.     xlSht = xlWb.Worksheets (1)
  8.  
  9.     xlSht.Cells(1,2).Value=str(Name)
  10.     xlSht.Cells(2,2).Value=str(Job)
  11.     xlSht.Cells(3,2).Value=int(Phone)
  12.     xlSht.Cells(5,2).Value=str(Month)
  13.  
  14.     try:
  15.         for i in range (9,40):
  16.             xlSht.Cells(i,2).Value=Analysing_Dict[i-8][0]
  17.             xlSht.Cells(i,3).Value=Analysing_Dict[i-8][1]
  18.             xlSht.Cells(i,4).Value=Analysing_Dict[i-8][2]
  19.     except:
  20.         pass
  21.  
  22.     xlSht.Cells(41,4).Value=Analysing_Dict["Totals"][0]
  23.     xlSht.Cells(42,4).Value=Analysing_Dict["Totals"][1]
  24.     xlSht.Cells(43,4).Value=Analysing_Dict["Totals"][2]
  25.  
  26.     #Preview or not....
  27.     if preview:
  28.         xlApp.Visible=1
  29.         xlApp.ActiveWindow.SelectedSheets.PrintPreview()
  30.  
  31.     else:
  32.         #Print The Sheet...
  33.         xlSht.PrintOut()        # This is for printing :)
  34.         xlApp.DisplayAlerts=False   # This is for NOT asking to save changes
  35.         xlApp.Workbooks.Close()
  36.         xlApp.DisplayAlerts=True # This is for bringing back the default behaviour
  37.         xlApp.Quit()
  38.  

伊莱亚斯

# 回答2


嗨,
它起作用了...太感谢你了..抱歉,回复晚了:-)

标签: python

添加新评论