Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get work sheet by name in Excel Object Model automation interface

Is there a way to use XlsxWriter to get a worksheet by name?

import win32com.client, types, pythoncom, sys, os, string
import xlsxwriter
xlApp = win32com.client.Dispatch("Excel.Application")
for file in os.listdir("C:\Users\\Desktop\Escel"):
    if file.endswith(".xlsx"):
        fileName = file
        books = xlApp.Workbooks.Open(r"C:\\Users\\Desktop\\Escel\\" + str(fileName))
        ws = books.sheet_by_name("2015 Data")
        #ws = books.Worksheets[0]
        ws.Visible = 1
        fileName.replace(".xlsx","")  
        ws.ExportAsFixedFormat(0, r"C:\\Users\\Desktop\\PDF\\" + str(fileName) + str(".pdf"))
        books.Close(True, r"C:\\Users\\Desktop\\Escel\\" + str(fileName))
like image 750
user3078335 Avatar asked Oct 26 '25 11:10

user3078335


1 Answers

XlsxWriter 0.8.7 now comes with a way to do that. For example:

worksheet = workbook.get_worksheet_by_name("Sheet1")

See: http://xlsxwriter.readthedocs.io/workbook.html#get_worksheet_by_name

like image 51
Chris Nielsen Avatar answered Oct 28 '25 04:10

Chris Nielsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!