Python Forum
AttributeError: 'str' object has no attribute 'name'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'str' object has no attribute 'name'
#1
Trying to grab an excel file from azure blob store and put into azure sql server instance. it was working and suddenly stopped. It was running on a coworkers machine and he is using 3.7 I am using 3.8.
Hoping someone can see something simple were missing.

Getting this error when I run it.

'table_name': tbl.name,
AttributeError: 'str' object has no attribute 'name'



import io
import requests
import openpyxl
import pandas as pd
from sqlalchemy import create_engine
import urllib


#Target Server Connection String
params = urllib.parse.quote_plus(r'Driver={ODBC Driver 17 for SQL Server};Server=servernamehere,1433;Uid=adminuser;Pwd=testpswd;database=Stage;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')


#Blob snapshot URL pulled from azure blob
input_excel = requests.get('https://testdata.blob.core.uscloudapi.net/testing6data/testing.xlsx, stream=True)

conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)

engn = create_engine(conn_str,echo=True)
# Loads the excel file into the eb variable
wb = openpyxl.load_workbook(filename=io.BytesIO(input_excel.content), data_only='True')

tables_dict = {}
# Go through each worksheet in the workbook
for ws_name in wb.sheetnames:
    #print(wb.sheetnames)
    print("")
    print(f"worksheet name: {ws_name}")
    ws = wb[ws_name]
    print(f"tables in worksheet: {len(ws._tables)}")
    # Get each table in the worksheet
    for tbl in ws._tables:
        # First, add some info about the table to the dictionary
        tables_dict[tbl.name] = {
                'table_name': tbl.name,
                'worksheet': ws_name,
                'num_cols': len(tbl.tableColumns),
                'table_range': tbl.ref}
        # Grab the 'data' from the table
        data = ws[tbl.ref]
        # Now convert the table 'data' to a Pandas DataFrame
        # First get a list of all rows, including the first header row
        rows_list = []
        for row in data:
            # Get a list of all columns in each row
            cols = []
            for col in row:
                cols.append(col.value)
                rows_list.append(cols)
        # Create a pandas dataframe from the rows_list.
        # The first row is the column names
        df = pd.DataFrame(data=rows_list[1:], index=None, columns=rows_list[0])
        # Add the dataframe to the dictionary of tables
        #  df = df.dropna(how='any')
        df.to_sql(tbl.name, engn, if_exists='append',schema='testing')
        print(df)
        tables_dict[tbl.name]['dataframe'] = df
print('-----------------------------done------------------------')
Reply
#2
Please show complete unmodified error traceback. (in error tags)
It contains valuable process information on what happened prior to error, and more.
Reply
#3
Sorry you can delete my post, it was just a simple version error with openpyxl
Reply
#4
When something stops working, it's useful to work out what changed in that time, to help narrow down the cause. Did the code change at all? Something in the environment? Does it work on one version of Python and not the other?
Reply
#5
Thread/Post Deletion
By posting on the forums you agree to the terms of the registration agreement of permitting us to retain that content for our database. You agree that any content you post is under the default MIT license allowing rights to use, copy, modify, merge, publish, distribute, sublicense, and/orThread/Post Deletion

see for complete forum rule: https://python-forum.io/misc.php?action=help&hid=32
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 755 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,657 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,460 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 738 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,842 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 726 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,298 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,547 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,883 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,448 Jul-29-2022, 09:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020