import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
data = np.random.rand(100, 100)
data
# Opens a new book
xw.view(data)
# Reuse an existing sheet (sheets gets cleared with every call)
xw.view(np.random.rand(5, 5), xw.sheets.active)
xw.view(np.random.rand(3, 3), xw.sheets.active)
import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
# Fire up a new book in the active Excel instance
wb1 = xw.Book()
# Connects to an unsaved book (looks in all Excel instances)
wb1 = xw.Book('Book2')
# Connects to a book by file name or full path and opens it if it is not open yet
# Windows: Use raw strings for path: r'C:\path\to\file.xlsx'
# Again: looks in all Excel instances
wb1 = xw.Book('C:\\Users\\MyWorkbook.xlsx')
import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
# app1 = xw.apps[pid]
app1 = xw.App()
app2 = xw.App()
# Open the same workbook twice in different Excel instances
app1.books.open('timeseries.xlsx')
app2.books.open('timeseries.xlsx')
# xw.Book('timeseries.xlsx') # this will throw an error
# The following syntax is *required* if the same file is open in >1 instances (full qualification)
print(app1.books['timeseries.xlsx'])
print(app2.books['timeseries.xlsx'])
print(app1.books['timeseries.xlsx'].app)
print(app2.books['timeseries.xlsx'].app)
import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
app1 = xw.App()
app2 = xw.App()
app1.books.open('timeseries.xlsx')
app2.books.open('timeseries.xlsx')
# Active Objects
# Active app
xw.apps.active
# active book in active app
xw.books.active
# active sheet in active book in active app
xw.sheets.active
# This is a special shortcut for interactive use only:
# It takes the active sheet from the active book
xw.Range('A1').value
import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
app1 = xw.App()
app2 = xw.App()
app1.books.open('timeseries.xlsx')
app2.books.open('timeseries.xlsx')
app2.kill()
# Table objects
import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt
import time
import sys
# NOTE: Excel Table objects aren't officially supported yet, but reading actually works nicely:
# The sample book has a table that was created with:
# Insert > Table (incl. Header Row and Total Row)
wb = xw.Book('table_objects.xlsx')
# wb = xw.Book('table_objects.xlsx')
sheet = wb.sheets[0]
# Get entire table body - no different from named range
sheet.range('Table1').value
# Get column data
sheet.range('Table1[Symbol]').value
# Get column including header and total rows
sheet.range('Table1[[#All], [Last]]').value
# Get header row for one column
sheet.range('Table1[[#Headers], [Last]]').value
# Total row for one column
sheet.range('Table1[[#Totals], [Last]]').value
# Two or more adjancent columns
sheet.range('Table1[[Index]:[Last]]').value
wb.close()
import csv
f = open("../data/sample-sales.csv",'rt')
reader = csv.reader(f)