熊猫将数据框导出到Excel工作表(链接部分可点击和列宽调整)

时间:2020-09-10 02:10:59

标签: python excel

我对Python还是很陌生,尤其是对于Excel操作。我已经使用以下代码生成了excel文件输出。但是,我发现了两个主要问题,已经搜索了很长时间,仍然无法解决。

第一个问题是我试图将第二个数据框追加到同一工作表中的第一个数据框之后,但是该第二个数据框的链接变得不可单击。

第二个问题是我无法调整列宽,以使所有文本都可见。

我衷心希望有人能就这两个问题给我建议。我搜索并尝试了许多方法,但结果是徒劳的。非常感谢大家。

excel输出: enter image description here

代码:

import twint 
import nest_asyncio
import pandas as pd
import numpy as np
import datetime
from openpyxl import load_workbook

pd.set_option('display.max_colwidth', -1)
pd.set_option('max_columns', None)
pd.set_option('display.max_rows', None)

nest_asyncio.apply()


c = twint.Config()
c.Search = "from:saxena_puru"
c.Pandas = True
c.Since = "2020-09-10 00:30:00"
twint.run.Search(c)

Tweets_df = twint.storage.panda.Tweets_df

Tweets_df.insert(1,'main tweet', '***')
Tweets_df.loc[Tweets_df['id'] == Tweets_df['conversation_id'], 'main tweet'] = "Yes"
Tweets_df = Tweets_df[['date','main tweet','tweet','link']]
Tweets_df.insert(1,'name','Puru Saxena')
Tweets_df['tweet'] = Tweets_df['tweet'].str.replace("\n","") # For notebook proper display only. Excel / csv display will not be affected.

Tweets_df.head()

writer = pd.ExcelWriter('portfolio.xlsx', engine='xlsxwriter')
writer.save()


writer = pd.ExcelWriter('portfolio.xlsx', engine='xlsxwriter')
Tweets_df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()


c = twint.Config()
c.Search = "from:richard_chu97"
c.Pandas = True
c.Since = "2020-09-09 00:30:00"
twint.run.Search(c)

Tweets_df = twint.storage.panda.Tweets_df

Tweets_df.insert(1,'main tweet', '***')
Tweets_df.loc[Tweets_df['id'] == Tweets_df['conversation_id'], 'main tweet'] = "Yes"
Tweets_df = Tweets_df[['date','main tweet','tweet','link']]
Tweets_df.insert(1,'name','Richard Chu')
Tweets_df['tweet'] = Tweets_df['tweet'].str.replace("\n","") # For notebook proper display only. Excel / csv display will not be affected.

writer = pd.ExcelWriter('portfolio.xlsx', engine='openpyxl')
writer.book = load_workbook('portfolio.xlsx')
writer.sheets = dict((ws.title, ws) for ws in writer.book.worksheets)
reader = pd.read_excel(r'portfolio.xlsx')
Tweets_df.to_excel(writer,index=False,startrow=len(reader)+3,header=False,sheet_name='Sheet1')

writer.save()

0 个答案:

没有答案
相关问题