如何下载xlsm文件并阅读python中的每张工作表? [英] How do I download an xlsm file and read every sheet in python?

查看:62
本文介绍了如何下载xlsm文件并阅读python中的每张工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在执行以下操作.

Right now I am doing the following.

import xlrd

resp = requests.get(url, auth=auth).content
output = open(r'temp.xlsx', 'wb')
output.write(resp)
output.close()
xl = xlrd.open_workbook(r'temp.xlsx')
sh = 1
try:
    for sheet in xl.sheets():
    xls.append(sheet.name)
except:
    xls = ['']

它正在提取工作表,但是我不知道如何读取文件,或者不知道是否将文件另存为.xlsx确实适用于宏.我所知道的是代码现在无法正常工作,我需要能够捕获在宏中生成的数据.请帮忙!谢谢.

It's extracting the sheets but I don't know how to read the file or if saving the file as an .xlsx is actually working for macros. All I know is that the code is not working right now and I need to be able to catch the data that is being generated in a macro. Please help! Thanks.

推荐答案

我强烈建议使用 xlwings ,如果您要打开,修改和保存 .xlsm 文件而不损坏它们.我尝试了很多不同的方法(使用其他模块,例如 openpyxl ),这些宏总是最终被破坏.

I highly recommend using xlwings if you want to open, modify, and save .xlsm files without corrupting them. I have tried a ton of different methods (using other modules like openpyxl) and the macros always end up being corrupted.

import xlwings as xw
app = xw.App(visible=False) # IF YOU WANT EXCEL TO RUN IN BACKGROUND

xlwb = xw.Book('PATH\\TO\\FILE.xlsm')
xlws = {}
xlws['ws1'] = xlwb.sheets['Your Worksheet']

print(xlws['ws1'].range('B1').value)   # get value
xlws['ws1'].range('B1').value =  'New Value'   # change value

yourMacro = xlwb.macro('YourExcelMacro')
yourMacro()

xlwb.save()
xlwb.close()

编辑-我添加了一个选项,以根据用户要求使Excel不可见

Edit - I added an option to keep Excel invisible at users request

这篇关于如何下载xlsm文件并阅读python中的每张工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆