excel 在 python 中记录
excel to doc in python
from docx.api import Document
import pandas as pd
document = Document("D:/tmp/test.docx")
tables = document.tables
df = pd.DataFrame()
for table in document.tables:
for row in table.rows:
text = [cell.text for cell in row.cells]
df = df.append([text], ignore_index=True)
df.columns = ["Column1", "Column2"]
df.to_excel("D:/tmp/test.xlsx")
print df
输出
`>>>
Column1 Column2
0 Hello TEST
1 Est Ting
2 Gg ff
如何删除行和列 0,1,2 以及如何在此代码中添加一些图像?
导出到excel时可以去掉索引和表头,只需添加以下条件:
df.to_excel("test.xlsx", header = None, index = False)
可以这样做
import pandas as pd
dataset = pd.DataFrame({'A':[1,2,3,4], 'B':[5,6,7,8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
dataset.to_excel(writer, sheet_name = 'Data', index = False, header = False)
sheet_name = 'Images' #Sheet name in which the image will be generated
cell = 'B2' #Position of the image in w.r.t cell value
workbook = writer.book
worksheet = workbook.add_worksheet(sheet_name)
worksheet.insert_image(cell, 'Tmp.jpg') #Add image
workbook.close()
writer.save()
from docx.api import Document
import pandas as pd
document = Document("D:/tmp/test.docx")
tables = document.tables
df = pd.DataFrame()
for table in document.tables:
for row in table.rows:
text = [cell.text for cell in row.cells]
df = df.append([text], ignore_index=True)
df.columns = ["Column1", "Column2"]
df.to_excel("D:/tmp/test.xlsx")
print df
输出
`>>>
Column1 Column2
0 Hello TEST
1 Est Ting
2 Gg ff
如何删除行和列 0,1,2 以及如何在此代码中添加一些图像?
导出到excel时可以去掉索引和表头,只需添加以下条件:
df.to_excel("test.xlsx", header = None, index = False)
可以这样做
import pandas as pd
dataset = pd.DataFrame({'A':[1,2,3,4], 'B':[5,6,7,8]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
dataset.to_excel(writer, sheet_name = 'Data', index = False, header = False)
sheet_name = 'Images' #Sheet name in which the image will be generated
cell = 'B2' #Position of the image in w.r.t cell value
workbook = writer.book
worksheet = workbook.add_worksheet(sheet_name)
worksheet.insert_image(cell, 'Tmp.jpg') #Add image
workbook.close()
writer.save()