Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将HTML表保存到excel时出现问题_Python_Excel_Pandas - Fatal编程技术网

使用Python将HTML表保存到excel时出现问题

使用Python将HTML表保存到excel时出现问题,python,excel,pandas,Python,Excel,Pandas,这是我第一次使用Python,我正在尝试刮取方法,并将网络上可用的代码放在一起,目前我一直在将输出保存到Excel文件中 好的,首先我需要从Outlook中读取一封电子邮件,并获取其中的数据。但它是表格式的,这意味着创建者将Excel中的数据复制粘贴为表,所以我发现最好的方法是将其转换为HTML文件 import win32com.client import xlsxwriter import pandas as pd import requests from bs4 import Beauti

这是我第一次使用Python,我正在尝试刮取方法,并将网络上可用的代码放在一起,目前我一直在将输出保存到Excel文件中

好的,首先我需要从Outlook中读取一封电子邮件,并获取其中的数据。但它是表格式的,这意味着创建者将Excel中的数据复制粘贴为表,所以我发现最好的方法是将其转换为HTML文件

import win32com.client
import xlsxwriter
import pandas as pd
import requests
from bs4 import BeautifulSoup

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
'''message = messages.GetLast()
body_content = message.Body
subject = message.Subject
categories = message.Categories
print(body_content)
print(subject)
print(categories)'''
string = "Monthly PPM Report"
for message in messages:
    if string in message.Subject:
        print(message.HTMLBody)
        Html_file= open("filename.html","w", encoding="utf-8")
        Html_file.write(message.HTMLBody)
        Html_file.close()
因此,使用上面的代码,我设法将电子邮件保存为HTML文件。下一步是查找以div类为目标的表

rfile  = open('filename.html')
rsoup  = BeautifulSoup(rfile)
nodes1  = rsoup.find('div',{'class':'MsoNormalTable'})
当我试图打印时,我设法得到了我需要的表格,但当我试图使用
nodes1.to_Excel('test.xlsx')
将其保存为Excel文件时,我得到了这个错误

nodes1.to_excel('test.xlsx')AttributeError:'NoneType'对象没有 属性“to_excel”

关于我缺少的步骤有什么建议吗?

要使用pandas To_excel()方法,首先需要一个pandas数据框

假设nodes1是字典对象:

data_frame = pd.DataFrame(data=nodes1)
data_frame.to_excel('label_name')
要使用pandas To_excel()方法,首先需要一个pandas数据框

假设nodes1是字典对象:

data_frame = pd.DataFrame(data=nodes1)
data_frame.to_excel('label_name')

您可以使用pandas函数read_html读取表格:

将熊猫作为pd导入
rfile=open('filename.html')
html=rfile.read()
#文件中的所有表格
tab_list=pd.read_html(html)
#带页眉的表格
tab_list=pd.read_html(html,header=0)
#具有属性的表
tab_list=pd.read_html(html,attrs={'class':'xxx','id':'xxx','align':“center”,'cellspacking':“1”,'cellpadding':“4”,'border':“0”})
#你在美丽之路的点点滴滴
tab_list=pd.read_html(str(nodes1))
#保存第一个表
选项卡列表[0]。到excel('test.xlsx')

您可以使用pandas函数read\u html读取表格:

将熊猫作为pd导入
rfile=open('filename.html')
html=rfile.read()
#文件中的所有表格
tab_list=pd.read_html(html)
#带页眉的表格
tab_list=pd.read_html(html,header=0)
#具有属性的表
tab_list=pd.read_html(html,attrs={'class':'xxx','id':'xxx','align':“center”,'cellspacking':“1”,'cellpadding':“4”,'border':“0”})
#你在美丽之路的点点滴滴
tab_list=pd.read_html(str(nodes1))
#保存第一个表
选项卡列表[0]。到excel('test.xlsx')

谢谢,所以问题是
节点1
不是字典,所以我需要在追加之前进行转换。谢谢,所以问题是
节点1
不是字典,所以我需要在追加之前进行转换。