Excel saved with openpyxl can't be opened in another system - python

we have a reporting system which display some dashboards, and we need to update a data table which has the numbers for the dashboard everyday. The problem is the excel file which is saved with openpyxl can't be opened in the reporting system, Everyday after updating the numbers in the excel file, I need to open the excel file and click "save" button, then upload the excel file to the reporting server, then the numbers in the excel file can be read. if I just upload the excel file to the server without doing anything with the excel file, then the server won't able to read data from it. what can be the problem? my code is:
wb = load_workbook('Datasource_klip.xlsx')
ws=wb["Day"]
ws['B%d'%(row_index)]=month
...
wb.save('Datasource_klip.xlsx')

Related

How to use Python win32 to automatically input a password when refreshing a pivot table

I have an Excel file with several pivot tables on various sheets, and each pivot table uses SQL Views as the data source. Because this is tied to the database, refreshing it will demand a password.
I have created python script to automate it using the win32com package.
import win32com.client
# Opening Excel software using the win32com
File = win32com.client.Dispatch("Excel.Application")
# Display Excel File
File.Visible = 1
# Opening my workbook
Workbook = File.Workbooks.open('my_xl.xlsx')
# Refeshing all the sheets
Workbook.RefreshAll()
# Saving the Workbook
Workbook.Save()
# Closing the Excel File
File.Quit()
print('Refresh Done')
My Questions:
The workbook will automatically open and request the password when I run the above script. I added this script Workbook.Unprotect.set password('secret') in an attempt to figure out how to unlock it. It's not functioning, and since I'm not very experienced with VBA, I'm not sure what the right syntax is for this.
To move on to the next phase, I manually encoded my password and a confimation box appears.
Is there a way to include this in my script so that it automatically clicks the "OK" button?

Trying to Automate Opening of Excel File and then flowing that data into Power BI (Used Selenium to web scrape exported xls file)

I have code using selenium that successfully exports an xls file from my desired website. Once the xls file is downloaded I am unsure how to use selenium to open the file. Once this data gets opened and downloaded I would like it to automatically be flowing into Power BI.
Any thoughts on how to achieve this?
You don't need Selenium. It's plain vanilla to directly access a web based Excel file with Power BI. This is what Power BI is made for.
However, if you already have an Excel file locally, I'd suggest you upload it to Onedrive and import it into Power BI from there. And once your report is in the Online service you can directly refresh it via schedule or trigger.
Importing the Excel file from disk is not the recommended way of doing things unless it's a one-off.

Python Sending Excel Attachment corrupts file

Using the ezgmail library, I am sending an E-mail with an Excel file attached.
I am able to send the E-mail with the attached Excel file, but when I open the Excel file from the sent E-mail, it seems it becomes corrupt for some reason because I get this message:
We found a problem with some content in 'attached_file.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
When I click "Yes" I get:
Excel cannot open the file 'attached_file.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
The excel file that I attach opens fine from my desktop, or if I E-mail it manually. I get these prompts only after E-mailing it via my python script which leverages ezgmail. Does anyone have ideas?

Accessing an Excel file in a .dtsx that was generated through Xlwt library (Python) raises error CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER

I have a web scraper written in Python, fetching raw data from the HTML of a page and writing it onto a 97-2003 Workbook Excel file, using the Xlwt library. I then have a .dtsx file with some tasks, where one of them is an Excel Source task to fetch data from an Excel file. Later down the road, that data is inserted into a SQL Server table.
If I try to access my newly-generated Excel file with said task, I get an OLE DB error
External table is not in the expected format
And I cannot run my dtsx. However, if I manually access the Excel file through my File Explorer, open it and close it again (don't even need to save it), suddenly my SSIS task works without a problem, fetching all the columns and all the info. What could possibly be causing this behavior?
External table is not in the expected format
The error above happens when the Excel file is corrupted and cannot be opened by Access Database Engine (OLE DB provider) even if you can open the file from Excel.
In general, the solution is to open this Excel manually which will auto repair it. In a similar case and if the process is repeated many times, you can automate opening and repairing excel using a C# script using Interop.Excel library.
Additional Information
What .xlsx file format is this?
Getting "External table is not in the expected format." error while trying to import an Excel File in SSIS

Scrape data in csv format without downloading csv file

I am trying to scrape data (using python) ,from google sheet, in csv format,without downloading the csv file. My aim is to update a database in mysql after getting information from google sheets.
Downloading the csv file would make me download the csv file again and again ,whenever the google sheet gets updated.
I have tried to use csv_reader(open('')) and putting the url of the google sheet in csv format(the link that we get after publishing to the web in csv format).
But it shows it as an invalid argument.
Can someone help me with this?

Categories