大家好,
我是新手,我已经写了非常基本和小的代码,我请求帮助以下人员.对于我的项目,我想写一个Python代码,它应该打开一个已经存在的Excel工作表,其中可以包含2或3个数据表和写数据在Sheet1(第一个工作表)单独,不应该修改其他数据表中的数据并保存它.
我听说人们说可以通过pyExcelerator、xlrd或xlw.因为所有这些东西对我来说都是新的..我真的不知道该怎么做.-(
因此,我请求你们所有人帮助我的样本代码,将尽可能早地完成上述任务.
谢谢,
阿木达
# 回答1
嗨!
我对Python并不陌生,所以下面是我用来做您所描述的事情的一个脚本示例:
(当然,您应该更改我使用的所有变量,并放置您自己的变量,
但由于我是在一些良好的研究(主要是在以下领域)提出的
VisualBasic...),我认为这对你来说是一个很好的起点.)
我希望这会有一点帮助...
- from win32com.client import Dispatch
-
- def Create_Analysis_Page(Name,Job,Phone,Month,Analysing_Dict,preview=False):
-
- xlApp = Dispatch ("Excel.Application")
- xlWb = xlApp.Workbooks.Open ("C:\\Python25\\Misthos.xlsx")
- xlSht = xlWb.Worksheets (1)
-
- xlSht.Cells(1,2).Value=str(Name)
- xlSht.Cells(2,2).Value=str(Job)
- xlSht.Cells(3,2).Value=int(Phone)
- xlSht.Cells(5,2).Value=str(Month)
-
- try:
- for i in range (9,40):
- xlSht.Cells(i,2).Value=Analysing_Dict[i-8][0]
- xlSht.Cells(i,3).Value=Analysing_Dict[i-8][1]
- xlSht.Cells(i,4).Value=Analysing_Dict[i-8][2]
- except:
- pass
-
- xlSht.Cells(41,4).Value=Analysing_Dict["Totals"][0]
- xlSht.Cells(42,4).Value=Analysing_Dict["Totals"][1]
- xlSht.Cells(43,4).Value=Analysing_Dict["Totals"][2]
-
- #Preview or not....
- if preview:
- xlApp.Visible=1
- xlApp.ActiveWindow.SelectedSheets.PrintPreview()
-
- else:
- #Print The Sheet...
- xlSht.PrintOut() # This is for printing :)
- xlApp.DisplayAlerts=False # This is for NOT asking to save changes
- xlApp.Workbooks.Close()
- xlApp.DisplayAlerts=True # This is for bringing back the default behaviour
- xlApp.Quit()
-
伊莱亚斯
# 回答2
嗨,
它起作用了...太感谢你了..抱歉,回复晚了:-)