1

I am still pretty new to Python, so perhaps I am missing something obvious. I am trying to download a simple spreadsheet from Google Docs, save the file, and open it in Excel. When I did a test run with text files instead of excel files, it worked fine. However, using xls and xlsx, when excel opens the newly downloaded file, it says that the data is corrupted. How can I fix this?

import urllib2

print "Downloading..."
myfile = urllib2.urlopen("https://docs.google.com/spreadsheet/pub?key=0AoJYUIVnE85odGZxVHkybGxYRXF1TFpuQXdqZlJwNXc&output=xls")
output = open('C:\\Users\\Lucas\\Desktop\\downloaded.xlsx', 'w')
output.write(myfile.read())
output.close()
print "Done"

import subprocess
subprocess.call(['C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.exe', 'C:\\Users\\Lucas\\Desktop\\downloaded.xlsx'])
4

2 回答 2

2

你想做它wb你可以看看这里的文档

于 2013-02-07T19:12:59.353 回答
0

您正在以纯文本、ascii 模式编写文件。Excel 文档不是纯文本:在这种假设下,您将错误地处理内容。

要按原样使用数据,对其格式的假设为零,请使用二进制模式。这里:

output = open('C:\\Users\\Lucas\\Desktop\\downloaded.xlsx', 'wb')

注意末尾的“b”标志。

于 2013-02-07T19:14:18.050 回答