473,654 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding Worksheets to an Excel Workbook

All

I'm a Python newbie, and I'm just getting to the wonders of COM
programming. I am trying to programmaticall y do the following:

1. Activate Excel
2. Add a Workbook
3. Add a Worksheet
4. Populate the new Worksheet
5. Repeat steps 3,4 while there is data.

How do you add a Worksheet to a Workbook?

Thanks!

Oct 10 '06 #1
4 14545
e.**********@ac centure.com wrote:
All

I'm a Python newbie, and I'm just getting to the wonders of COM
programming. I am trying to programmaticall y do the following:

1. Activate Excel
2. Add a Workbook
3. Add a Worksheet
4. Populate the new Worksheet
5. Repeat steps 3,4 while there is data.

How do you add a Worksheet to a Workbook?
To find out how to do things, you can:

(1) use the VBA help in Excel.

You would find (eventually):
"""
Add method as it applies to the Sheets and Worksheets objects.

Creates a new worksheet, chart, or macro sheet. The new worksheet
becomes the active sheet.

expression.Add( Before, After, Count, Type)
expression Required. An expression that returns one of the above
objects.

Before Optional Variant. An object that specifies the sheet before
which the new sheet is added.

After Optional Variant. An object that specifies the sheet after
which the new sheet is added.

Count Optional Variant. The number of sheets to be added. The default
value is one.

Type Optional Variant. Specifies the sheet type. Can be one of the
following XlSheetType constants: xlWorksheet, xlChart,
xlExcel4MacroSh eet, or xlExcel4IntlMac roSheet. If you are inserting a
sheet based on an existing template, specify the path to the template.
The default value is xlWorksheet.

Remarks
If Before and After are both omitted, the new sheet is inserted before
the active sheet.
"""
so,
your_handle.Wor ksheets.Add()
looks like what you need.

(2) Again in Excel, use the "record a macro" facility: turn on
recording, do your thing, stop recording, inspect the generated macro.

In this case, this gave
Sheets.Add
which you translate to
your_handle.She ets.Add()

What's the difference between Sheets and Worksheets? I dunno. Try both.
Look in the Excel VBA help.

HTH,
John

Oct 10 '06 #2
I expect this doesn't help him much. I get the impression he is looking
more for a recipe.

Just doing a Google search of python + excel I got the following which
make some good starting points:

http://www.markcarter.me.uk/computing/python/excel.html
http://mail.python.org/pipermail/pyt...er/183367.html
http://mathieu.fenniak.net/plotting-...ugh-pythoncom/

There are lots of others.

Mark

* John Machin wrote (on 10/10/2006 2:59 PM):
e.**********@ac centure.com wrote:
>All

I'm a Python newbie, and I'm just getting to the wonders of COM
programming. I am trying to programmaticall y do the following:

1. Activate Excel
2. Add a Workbook
3. Add a Worksheet
4. Populate the new Worksheet
5. Repeat steps 3,4 while there is data.

How do you add a Worksheet to a Workbook?

To find out how to do things, you can:

(1) use the VBA help in Excel.

You would find (eventually):
"""
Add method as it applies to the Sheets and Worksheets objects.

Creates a new worksheet, chart, or macro sheet. The new worksheet
becomes the active sheet.

expression.Add( Before, After, Count, Type)
expression Required. An expression that returns one of the above
objects.

Before Optional Variant. An object that specifies the sheet before
which the new sheet is added.

After Optional Variant. An object that specifies the sheet after
which the new sheet is added.

Count Optional Variant. The number of sheets to be added. The default
value is one.

Type Optional Variant. Specifies the sheet type. Can be one of the
following XlSheetType constants: xlWorksheet, xlChart,
xlExcel4MacroSh eet, or xlExcel4IntlMac roSheet. If you are inserting a
sheet based on an existing template, specify the path to the template.
The default value is xlWorksheet.

Remarks
If Before and After are both omitted, the new sheet is inserted before
the active sheet.
"""
so,
your_handle.Wor ksheets.Add()
looks like what you need.

(2) Again in Excel, use the "record a macro" facility: turn on
recording, do your thing, stop recording, inspect the generated macro.

In this case, this gave
Sheets.Add
which you translate to
your_handle.She ets.Add()

What's the difference between Sheets and Worksheets? I dunno. Try both.
Look in the Excel VBA help.

HTH,
John
Oct 10 '06 #3
# here is a simple script:

from win32com.client import Dispatch

xlApp = Dispatch("Excel .Application")
xlApp.Visible=1 #show me excel
xlApp.Workbooks .Add() #add a workbook

for r in range(1,5): #put data into spreadsheet row/column
xlApp.Cells(r,r ).Value=r

for r in range(1,5): #read data from sheet
print xlApp.Cells(r,r ).Value

xlApp.Worksheet s.Add()#add another sheet in same workbook.

for r in range(1,11): #put data into spreadsheet
xlApp.Cells(1,r ).Value=r #first row this tome

#xlApp.ActiveWo rkbook.Close()
#xlApp.Quit

Good luck.

Here is a free Excel Object Model Overview:
http://msdn.microsoft.com/library/de...eloverview.asp

Oct 10 '06 #4
These are all excellent suggestions. Thanks everyone for your help!

Oct 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
25253
by: Geert-Pieter Hof | last post by:
Hello, My VB 6.0 application read and writes data from and to a MS Excel workbook, using the Microsoft.Jet.OLEDB.4.0 provider. Now I want to protect the Excel workbook with a password, but I figured out that it is not possible to open the workbook for data access with ADO (http://support.microsoft.com/?KBID=211378). Is there another way to use a password protected Excel workbook in my
2
5095
by: WZ | last post by:
I used the following code to create a workbook and save it in a webapplication. dim oExcel As Excel.ApplicationClass dim oBook As Excel.WorkbookClass dim obooks As Excel.Workbooks dim designb As Excel.Workbook dim osheets As Excel.Sheets dim result as string
7
4092
by: Pierre | last post by:
Hi, Tryin to use this method : MyExcelObject.Application.ActiveWorkBook.set_Colors(int index, object RHS). But really don't know what this RHS is ? Any ideas ? Thks for help
0
1692
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a single worksheet into a dataset. I successfully built a dynamic datagrid. And I did successfully bind it to the dataset. Then I added the datagrid control to a PlaceHolder. The first worksheet displays beautifully. The next step is to allow...
5
8032
by: Iris | last post by:
I have 8 text files (tab delimited) that I would like to import into an Excel workbook as 8 individual worksheets but I cannot find any example code on this subject. Can anyone help me please???? Thanks in advance. Regards,
1
1679
by: david shapiro | last post by:
I've created an excel workbook with several worksheets. In each worksheet, I've highlighted a number of the cells containing key statistics in red. Is there any easy one-step way that I can pull all of the cells highlighted in red out of all of the worksheets in the workbook and put them all in one separate worksheet? Or is there a macro you might suggest? Unfortunately, the red cells are not in any systematic order in the
1
1279
by: Lucile | last post by:
Hi all, I am trying to open a Excel Workbook to check data in worksheets. The ONLY info I have about the EXCEL file is its entire path. I tried to do the following, but an exception occurs: Dim _objXLApp As Excel.ApplicationClass Dim _objXLFile As Excel.Workbook Dim _objWorkSheets As Excel.Worksheets
0
2423
by: Tony2299 | last post by:
How to export data using ASP to excel workbook (with multiple worksheets)? I created an excel workbook with 5 worksheets and each one has a name range. Using asp I insert data to individual worksheet. The problem is that excel treat all numbers as string/text when I do: rsExcel.Fields(0).Value = 5 When I open an excel file the value 5 is display as "5" If I use application/vnd.ms-excel method I can get the value display correctly...
10
41464
by: John Brock | last post by:
My VB.NET program pops up an Excel workbook for the user. If I initially create the workbook using: Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet) I end up with a workbook named "sheet1". If I use an existing Excel file as a template, e.g.: Workbooks.Add("C:\temp\MyTemplateFile.xls")
3
13036
by: mike11d11 | last post by:
I was able to create three worksheets in my workbook, but when I go to add the 4th I get an Invalid Index error. I must be leaving something out to when adding 4 or more sheets. Thanks Dim oExcel As Object Dim oBook As Object Dim oSheet1500 As Object Dim oSheetARPPD As Object Dim oSheetDFDDNA As Object Dim oSheetDNE As Object
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.