Bottom Page

Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
win32com Trying to open a xlmb file with macros
#1
Hi guys, I am trying to read data from a xlsb file using the code below
import win32com.client
excel = win32com.client.Dispatch("Excel.Application")
excel.DisplayAlerts = False
excel.Visible=False
#doc1 = excel.Workbooks.Open.
doc = excel.Workbooks.Open("O:\xxx\xxx\xxx.xlsb")
doc.SaveAs(Filename="O:\\xxx\\xxx\\xxx.csv",FileFormat=6)
doc.Close()
excel.Quit()
The problem is that it does appear a mask with
Error:
RunTimeError '1004' Cannot Run the macro....
Is there any way just to read the data in Excel without the need to run any macro?

Thanks
You like this post
Quote
#2
Use code tag,i have added in your post.
win32com is now very little used for excel connection,now is XLSB a binary Microsoft Excel format.
There is a library for this pyxlsb that read XLSB.
Then can also read into pandas,for greater power if need edit.
Common Excel Tasks Demonstrated in Pandas
import pandas as pd
from pyxlsb import open_workbook as open_xlsb

df = []

with open_xlsb('some.xlsb') as wb:
    with wb.get_sheet(1) as sheet:
        for row in sheet.rows():
            df.append([item.v for item in row])

df = pd.DataFrame(df[1:], columns=df[0])
You like this post
Quote
#3
Thanks a lot snippsat,I am an absolute beginner, only working in Python in a week.
However, I changed the file to xlsm and now I am using xlrd

import xlrd
file_location='O:\xxx\xxxx\xxx Python.xlsm'
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(1)
sheet.cell_value(0,0)
sheet.nrows
sheet.ncols
for col in range(sheet.ncols):
print (sheet.cell_value(3, col))

It's reading the data without triggering the macro, but now I need to write the data to a csv file and struggling to understand how using the for. Is there a way to give a range to set a loop in print (sheet.cell_value(3, col)) and to use a variable instead of print?

Many Thanks
You like this post
Quote
#4
Hi guys, I am now using the code below:
import xlrd
file_location='O:\xxx\xxxxxx\xxx.xlsm'
workbook = xlrd.open_workbook(file_location)
#sheet = workbook.sheet_by_index(2)
xl_sheet =workbook.sheet_by_name("B Data")

for rownum in range(xl_sheet.nrows):
print (xl_sheet.row_values(rownum))

It reads the data from an Excel file which uses a third part add in that update the data. The point is that I only see the original data but not the updated. Let's say that the Cell 1 Col A has 20 as value, this is what Python shows me. Now, if I see the file on the machine with the plug in installed the value is 22 but the code in Python still shows me 20. Any idea how can I set a process to see the data when it is changed and not only the original one?

Thanks
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help with try and open 6.txt file and print as perfect or not Pleiades 13 243 Jan-03-2019, 10:14 PM
Last Post: Pleiades
  Firefox Selenium (open new tab) oneclick 1 122 Dec-29-2018, 06:59 AM
Last Post: hbknjr
  Unable to open file ayaz786amd 7 267 Nov-21-2018, 05:31 AM
Last Post: ayaz786amd
  PyPDF2 Hanging When Trying to Open Corrupted PDF bmccollum 6 229 Nov-09-2018, 10:40 AM
Last Post: Larz60+
  how to open dx file in Python jiaming 8 244 Oct-24-2018, 07:14 AM
Last Post: jiaming
  Python win32com add image in HTML body of email Joe_Dananza 0 749 Sep-09-2018, 02:48 PM
Last Post: Joe_Dananza
  Open file (map) in python LB_994 1 325 Aug-21-2018, 08:34 PM
Last Post: Gribouillis
  win32com Events not catching dageci 0 276 Aug-06-2018, 03:18 PM
Last Post: dageci
  open a web page by selenium !! evilcode1 3 392 Aug-01-2018, 03:05 PM
Last Post: snippsat
  Cannot open file in an OpenSharedItem() iterator - The file may not exist/may be open S3RG388 3 565 Jul-25-2018, 10:18 AM
Last Post: gontajones

Forum Jump:


Users browsing this thread: 1 Guest(s)