Validation and Drop Down in python - python

I'm attempting to teach myself python skills and took an awesome tutorial from Giraffe Academy and using some of the skills.
I created a file called country.xlsx and installed xlsxwriter to read, validate and create a dropdown box using this tutorial - https://xlsxwriter.readthedocs.io/example_data_validate.html
When I run or debug the code below,
import xlsxwriter
workbook = xlsxwriter.workbook("Countries.xlsx")
worksheet = workbook.sheet_by_name("Sheet1")
workbook = worksheet.set_column("A:A")
workbook = worksheet.set_column("B:B")
workbook = worksheet.set_column("C:C")
workbook = worksheet.set_column("D:D")
workbook - worksheet.set.row(0, 6)
heading1 = "Continent"
heading2 = "Country"
heading3 = "Capital"
heading4 = "Airline"
workbook = worksheet("A1", {heading1})
workbook = worksheet("B1", {heading2})
workbook = worksheet("C1", {heading3})
workbook = worksheet("D1", {heading4})
txt = "Select from the Dropdown List"
workbook = worksheet.data_validation("B15", {"validate": "list", "source" : "=$A$1:$D$7"})
workbook.close()
I receive this error
workbook = xlsxwriter.workbook('Countries.xlsx')
TypeError: 'module' object is not callable
Can someone point me in the right direction??

Looking at the Documentation it seems like you have to use Workbook with a capital W:
workbook = xlsxwriter.Workbook('Countries.xlsx')

Related

How write text on an existing xlsx file with the help of openpyxl on python?

Most of the tutorials are outdated and I am unsuccessful at writing on an xlsx file with the help of python.
Here is some code I used:
import openpyxl
wb = openpyxl.load_workbook('C:/Users/user/Desktop/pytranslate.xlsx')
path = 'C:/Users/user/Desktop/pytranslate.xlsx'
ws = wb['Sheet1']
ws.cell(row = 1,column = 1,value = 'need for meas')
val1 = ws['A1'].value
print(val1)
wb.save(path)
The program ends with no error but I cannot see any output on the xlsx file.
I used this code to test. I created an empty excel document first. It updated correctly.
import openpyxl
path = 'pytranslate.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb['Sheet1']
ws.cell(row = 1,column = 1,value = 'need for meas')
val1 = ws['A1'].value
print(val1)
wb.save(path)
Output (excel)
If your document is not updating, confirm you have correct permissions to the file and folder.

Error 'Workbook' object has no attribute 'add_vba_project'

I've successfully Extracted vbaProject.bin but I'm still getting an error. My goal is to be able to use VBA after the information is passed to the xlsm file. This is a workaround provided by xlswriter.
import pandas as pd
def export(self):
sql = "SELECT WellID,AFEno,AFEsuppno,AccountCode,AFEAmount FROM WellAFEDetails WHERE WellID = ?"
df = pd.read_sql_query(sql,self.con, params=([self.wellid_bx.get()]))
writer = pd.ExcelWriter (r'C:\Users\bjenks\Desktop\Macro.xlsm')
df.to_excel(writer, sheet_name='Raw')
workbook = writer.book
workbook.filename = (r'C:\Users\bjenks\Desktop\Macro.xlsm')
workbook.add_vba_project(r'C:\Users\bjenks\Desktop/vbaProject.bin')
writer.save()

Adding Excel Sheets to End of Workbook

I am trying to add excel worksheets to the end of a workbook, reserving the first sheet for a summary.
import win32com.client
Excel = win32com.client.DispatchEx('Excel.Application')
Book = Excel.Workbooks.Add()
Excel.Visible = True
Book.Worksheets(3).Delete()
Book.Worksheets(2).Delete()
Sheet = Book.Worksheets(1)
Sheet.Name = "Summary"
Book.Worksheets.Add(After=Sheet)
Sheet = Book.Worksheets(2)
Sheet.Name = "Data1"
This code adds the new sheet to the left, despite using After=Sheet, and when I modify the sheet named "Data1", it overwrites the sheet named "Summary".
This is similar to this problem:
Adding sheets to end of workbook in Excel (normal method not working?)
but the given solutions don't work for me.
Try using this by adding Before = None:
add = Book.Sheets.Add(Before = None , After = Book.Sheets(book.Sheets.count))
add.Name = "Data1"
Try using Sheet = excelApp.ActiveSheet:
Book.Worksheets.Add(After=Sheet)
Sheet = Book.ActiveSheet
Sheet.Name = "Data1"
import win32com.client as win32
xl = win32.gencache.EnsureDispatch('Excel.Application')
xl.Sheets.Add(After=xl.ActiveSheet).Name ="Name_of_your_Sheet"

openpyxl create a function that references a cell in another sheet

I just started working with openpyxl a couple of days ago and its a great library. However, the documentation seems to be sparse for advanced features. I have a couple of issues.
openpyxl seems to change the formula that I insert to a lower case which results in an unknown reference from excel.
furthermore, i changed the name of the sheet to accomidate the lowercase and still found a #NAME? error in the cell where the reference was at.
Can someone please show me how or where to find out how to reference a cell from another sheet in openpyxl
import openpyxl.Workbook
wb = Workbook()
ws = wb.get_active_sheet()
#shows up lowercase with name error in excel
ws.cell('A1).value = "$'Sheet'.E7 + 123"
#still shows a name error in excel
ws.cell('A2').value = "$'sheet'.E7 + 123"
Try this:
from openpyxl import Workbook
wb = Workbook()
ws = wb.create_sheet()
ws.title ='NewSheet'
ws.cell('E7').value = 7
ws = wb.create_sheet()
ws.cell('A1').value = "=NewSheet!E7 + 123"
wb.save( filename = 'temp2.xlsx' )
from openpyxl import Workbook, utils
wb = Workbook()
ws = wb.create_sheet()
ws.title ='NewSheet'
ws.cell('E7').value = 7
ws = wb.create_sheet()
ws.cell('A1').value = f"={utils.quote_sheetname(ws.title)}!E7 + 123"
wb.save( filename = 'temp2.xlsx' )
The problem with the previous answer is that it's dependant on the title of the sheet being 'NewSheet'. Using quote_sheetname()controls that.

Python win32com - Read text in a text box to a cell?

I would like to read the text from a text box in an Excel File and save that value to a variable. The problem I am having is with the reading of the TextBox. I have tried several methods, this one showed the most promise, as it does not generate an error, but it does not elicit the desired result either. Any suggestions are appreciated. See code below.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("C:\\users\\khillstr\\Testing\\Scripts\\Book1.xlsx")
excel.Visible = False
ws = wb.Worksheets
canvas = excel.ActiveSheet.Shapes
for shp in canvas.CanvasItems:
if shp.TextFrame.Characters:
print shp.TextFrame.Characters
else:
print "no"
Canvas has to do with graphics in excel files. I think you want access to the cells. Below is code that prints out each row as a tuple.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("C:\\users\\khillstr\\Testing\\Scripts\\Book1.xlsx")
excel.Visible = False
sheet = wb.Worksheets(1)
for row in sheet.UsedRange.Value:
print row
To get the text in a textbox object on a sheet you need to use shp.TextFrame.Characters.Caption as the Characters method returns a Characters object and not a string.
import win32com.client as win32
file_name = 'path_to_excel'
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(self.file_name)
excel.Visible = False
sheet = wb.Worksheets(1)
deep = lambda r,c: sheet.Cells(r,c)
print(deep(row_num,col_num))
excel.Application.Quit()
This code will open an excel located at 'path_to_excel' and read a cell located at (Row_Number = row_num, Column_Number = col_num)

Categories

Resources